Linux split column into two different columns in one CSV file

Hi I have a csv file with below entries

11
22
13
,,
aa
bb
cc
,,
ww
kk
ll
,,

      

Please suggest me a linux command or a script that can split this colomun into 3 columns in the same file as below

11  aa  ww
22  bb  kk
13  cc  ll

      

+3


source to share


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


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


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


This might work for you:

pr -tT3 -s\  file | sed \$d

      

0


source


with awk

awk 'BEGIN {RS=",,\n"; FS="[\n]"}{ }{a=a$1" ";b=b$2" ";c=c$3" ";} END{print a"\n"b"\n"c}' temp.txt

      

Output

11 aa ww
22 bb kk
13 cc ll

      

0


source







All Articles