How to add a space after a comma if it does not exist within the 6th column in a csv file?
How to add a space after a comma if it does not exist within the 6th column in a csv file?
Ubuntu 16.04
Bash 4.3.3
I also need a way to add a space after the comma if one does not exist in the 6th column. I had to comment the above line because it placed a space after all commas in the csv file.
Wrong: "This is 6th column,Hey guys,Red White & Blue,I know it,Right On"
"This is 6th column,Hey guys,Red White & Blue,I know it,Right On"
Perfect: "This is 6th column, Hey guys, Red White & Blue, I know it, Right On"
"This is 6th column, Hey guys, Red White & Blue, I know it, Right On"
I could almost see awk
printing out the 6th column then having sed
do the rest:
awk
sed
awk ' print $6 ' "$feed " | sed -i 's/|/,/g; s/,/, /g; s/,s+/, /g'
awk ' print $6 ' "$feed " | sed -i 's/|/,/g; s/,/, /g; s/,s+/, /g'
This is what I have so far:
for feed in *; do
sed -r -i 's/([^,]0,10)[^,]*/1/5' "$feed"
sed -i '
s/<b>//g; s/*//g;
s/([0-9])""/1inch/g;
# s/|/,/g; s/,/, /g; s/,s+/, /g;
s/"one","drive"/"onetext","drive"/;
s/"comments"/"description"/;
s/"features"/"optiontext"/;
' "$feed"
done
s/|/,/g; s/,/, /g; s/,s+/, /g;
works but is global and not within a column.
s/|/,/g; s/,/, /g; s/,s+/, /g;
Why not
awk ' print $6 ' "$feed " | sed 's/, */, /g'
? What's the |
doing? And all the other substitutes?– Ljm Dullaart
Sep 9 '18 at 21:25
awk ' print $6 ' "$feed " | sed 's/, */, /g'
|
2 Answers
2
It sounds like all you need is this (using GNU awk for FPAT):
awk 'BEGIN"[^"]+""; OFS="," gsub(/, ?/,", ",$6) 1'
e.g.:
$ cat file
1,2,3,4,5,"This is 6th column,Hey guys,Red White & Blue,I know it,Right On",7,8
$ awk 'BEGIN"[^"]+""; OFS="," gsub(/, ?/,", ",$6) 1' file
1,2,3,4,5,"This is 6th column, Hey guys, Red White & Blue, I know it, Right On",7,8
It actually looks like your whole shell script including multiple calls to GNU sed could be done far more efficiently in just one call to GNU awk with no need for a surrounding shell loop, e.g. (untested):
awk -i inplace '
BEGIN"[^"]+""; OFS=","
$0 = gensub(/([^,]0,10)[^,]*/,"\1",5)
$0 = gensub(/([0-9])""/,"\1inch","g")
sub(/"one","drive"/,""onetext","drive"")
sub(/"comments"/,""description"")
sub(/"features"/,""optiontext"")
gsub(/, ?/,", ",$6)
' *
but, if input contains blank fields
FPAT
will fail right ?– oguzismail
Sep 10 '18 at 5:13
FPAT
@oguzismail. No. The first part of the
FPAT
allows empty fields.– kvantour
Sep 10 '18 at 9:06
FPAT
@ed, This worked well with what you have. I'm going to implement it tomorrow.
– Curious Sam
Sep 17 '18 at 1:42
This might work for you (GNU sed):
sed -r 's/[^,"]*("[^"]*")*/n&n/6;h;s/, ?/, /g;G;s/.*n(.*)n.*n(.*)n.*n/21/' file
Surround the 6th field by newlines. Make a copy of the line. Replace all commas followed by a possible space with a comma followed by a space. Append the original line and using pattern matching replace the amended field discarding the rest of the ameliorated line.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You want to add a space after a comma if it doesn't exists? The comma has to exists to add a space after it. How does the input file look like? How is it separated? By spaces, so awk can parse it? What is the expected output? @edit och I get it, you want to add a space after a comma it the space does not exists, sry.
– Kamil Cuk
Sep 9 '18 at 20:18