Linux split column into two different columns in one CSV file
5 answers
You can do this with awk
.
Create a file named script.awk
with the following content:
BEGIN {
line = 0; #Initialize at zero
}
/,,/ { #every time we hit the delimiter
line = 0; #resed line to zero
}
!/,,/{ #otherwise
a[line] = a[line]" "$0; # Add the new input line to the output line
line++; # increase the counter by one
}
END {
for (i in a )
print a[i] # print the output
}
Run the file as follows:
awk -f test.awk < datafile
Output:
$ cat datafile 11 22 13 ,, aa bb cc ,, ww kk ll ,, $ awk -f script.awk < datafile 11 aa ww 22 bb kk 13 cc ll
Or, if you just want a one-liner , do this:
awk 'BEGIN{line=0;}/,,/{line=0;}!/,,/{a[line++]=a[line]" "$0;}END{for (i in a ) print a[i]}' datafile
EDIT:
This will add a comma between the fields:
awk 'BEGIN{line=0;}/,,/{line=0;}!/,,/{a[line++]=a[line]?a[line]","$0:$0;}END{for (i in a ) print a[i]}' datafile
# ^ This is the part that I changed
+2
source to share
Here's a nice crazy pipeline that actually does what the OP wants :!
#% cat t
11
22
13
,,
aa
bb
cc
,,
ww
kk
ll
,,
Then
#% pr -t -3 -l 4 -s' ' t | sed '$d'
11 aa ww
22 bb kk
13 cc ll
I'm sure better than commands without resorting to code.
EDIT Thanks to @ user000001 for the heads-up on my mistake, forcing me back to my solution.
+1
source to share
perl -lne 'if(/,,/){$.=0}$a{$.}=$a{$.}." ".$_ if($.!=0);END{foreach (sort keys %a){print $a{$_}}}'
Tested below:
> cat temp
11
22
13
,,
aa
bb
cc
,,
ww
kk
ll
,,
> perl -lne 'if(/,,/){$.=0}$a{$.}=$a{$.}." ".$_ if($.!=0);END{foreach (sort keys %a){print $a{$_}}}' temp
11 aa ww
22 bb kk
13 cc ll
>
0
source to share