Skip to main content
1 of 3
EarthIsHome
  • 295
  • 2
  • 13

Extract lines into separate files between two non-empty fields in the same column

I've got a csv mappings.csv shown below. I'd like to extract a block of records into separate files relating to each record that has a non-empty first field. The files are shown after mappings.csv.

$ cat mappings.csv TEST1,,,a,a,a,a ,,,b,b,b,b ,,,c,c,c,c TEST2,,,aa,aa,aa,aa ,,,bb,bb,bb,bb ,,,cc,cc,cc,cc ,,,dd,dd,dd,dd TEST3,,,aaa,aaa,aaa,aaa ,,,bbb,bbb,bbb,bbb 

Output files based on mappings.csv is below:

$ cat TEST1.csv TEST1,,,a,a,a,a ,,,b,b,b,b ,,,c,c,c,c 
$ cat TEST2.csv TEST2,,,aa,aa,aa,aa ,,,bb,bb,bb,bb ,,,cc,cc,cc,cc ,,,dd,dd,dd,dd 
$ cat TEST3.csv TEST3,,,aaa,aaa,aaa,aaa ,,,bbb,bbb,bbb,bbb 

I'm able to use awk to print lines that have a non-empty first field, but can't figure out how to expand and print subsequent records up to the next non-empty first field:

$ awk -F',' '$1' mappings.csv TEST1,,,a,a,a,a TEST2,,,aa,aa,aa,aa TEST3,,,aaa,aaa,aaa,aaa 

The other aspect of this problem is separating out the results into separate files. One thing I could do is use the matching record numbers to print out lines between the two. Something kind of like this:

$ awk -F',' '$1 {print NR}' mappings.csv 1 4 8 
EarthIsHome
  • 295
  • 2
  • 13