Reputation: 487
I'm trying to concatenate strings from a .csv
file.
A simple example data file is:
$ cat a
100, 200, 300
400, 500, 600
And the first approach results in nothing.
$ awk 'BEGIN{FS=",";OFS="-"}{gsub(/ /, "");a=$1$2$3; print $a}' a
In the second approach I also did not get the expected result.
$ awk 'BEGIN{FS=",";OFS="-"}{gsub(/ /, "");a="$1$2$3"; print $a}' a
100,200,300
400,500,600
Expected result would be
100200300
400500600
If the `.csv' file has strings instead of numbers, I still don't get the expected result. Some commas (,) are inserted in the middle, even though I've set the output field separator as dash (-).
$ cat b
AAA, BBB, CCC
DDD, EEE, FFF
$ awk 'BEGIN{FS=",";OFS="-"}{gsub(/ /, "");a=$1$2$3; print $a}' b
AAA,BBB,CCC
DDD,EEE,FFF
In this case, the expected result was
AAABBBCCC
DDDEEEFFF
Upvotes: 0
Views: 3675
Reputation: 2819
echo '100, 200, 300
400, 500, 600' |
awk NF++ FS=', *' OFS= # if you want it to auto-skip blank lines
awk ++NF FS=', *' OFS= # all lines printed, even blanks
100200300
400500600
Upvotes: 0
Reputation: 1517
awk -F, '{gsub(/ /,""); print $1$2$3}' file
100200300
400500600
Upvotes: 0
Reputation: 41446
Try this:
awk -F', *' -v OFS= '{$1=$1}1' a
100200300
400500600
awk -F', *' -v OFS= '{$1=$1}1' b
AAABBBCCC
DDDEEEFFF
Set Field Separator to comma and zero or more spaces and Output Fields Separator to nothing, then recreate the fields and print them.
Or just remove comma space
awk '{gsub(/, */,"")}1' a
sed 's/, *//g' a
Upvotes: 2
Reputation: 487
It was just an extra $
.
$ awk 'BEGIN{FS=",";OFS="-"}{gsub(/ /, "");a=$1$2$3; print a}' a
100200300
400500600
Upvotes: 0