Reputation: 62644
I have a gigantic 300MB text file that is a pipe delimited CSV.
Some Writing, Is|Another Field|Anotherfie,ld.
Some Writing, Is|Another Field|Anotherfie,ld.
Is there a way in bash shell to convert it to:
"Some Writing, ","Another Field","Anotherfie,ld."
Upvotes: 3
Views: 7486
Reputation: 2863
You can use sed
and it's substitution commands:
s/^/"/
will replace (insert) a "
sign at the beginning of each line.
s/|/","/g
will replace each |
character with ","
triplet. Note the g
letter at the end of command, which instructs it to replace every |
occurrence, not only the first one.
And finally:
s/$/"/
will replace (append) a "
sign at the end of each line.
So final command will be:
`cat filename.in | sed 's/^/"/;s/|/","/g;s/$/"/` > filename.out`
Upvotes: 5
Reputation: 88654
With sed:
sed 's/^/"/;s/|/","/g;s/$/"/' file
Output:
"Some Writing, Is","Another Field","Anotherfie,ld." "Some Writing, Is","Another Field","Anotherfie,ld."
If you want to edit your file "in place" add sed's option -i
.
Upvotes: 5
Reputation: 785276
Using awk you can do this:
awk -F '|' -v OFS=, '{for(i=1; i<=NF; i++) $i="\"" $i "\""} 1' file.csv
"Some Writing, Is","Another Field","Anotherfie,ld."
"Some Writing, Is","Another Field","Anotherfie,ld."
Upvotes: 2