Divide data with specific column values into separate files

hello!
i need a little help from you :slight_smile: ... i need to split a file into separate files depending on two conditions using scripting. The file has no delimiters. The conditions are col 17 = "P" and col 81 = "*", this will go to one output file; col 17 = "R" and col 81 = " ". Here is an example.

132855300       PASSOCIATED ASSOCIATEDL091000022160233784638     075911600000003*0D
140422500       PCHAD NOBASIC PT LTD   0759179119201017796       075911600000004*0D
144243100       RWASHINGTON CARDWELT SC0759061710110013401       075911600000001 17
40402357604     RMD ADVICE       95148R27507971438278102         075000020000002*0D
48412698886     RTHOMAS KLOEH  1918883R07591176887066770         075000020000003 0D
41406992615     RSAMIHA KADAD 26174726Y07500001900000000044697352075000020000001 13
 

Thanks in advance for your help,

Hi, try this:

awk '{a=substr($0,17,1); b=substr($0,81,1)} a=="P" && b=="*"{print > "outfile1"} a=="R" && b==" "{print > "outfile2"}' infile

or:

awk 'sub(/^.{16}P.{63}\*/,"&"){print > "outfile1371a"} sub(/^.{16}R.{63} /,"&"){print > "outfile1371b"}' infile

( if your awk is GNU awk it needs to be called as awk --posix )

Or perhaps your awk can do this:

awk '$17=="P" && $81=="*"{print > "outfile1"} $17=="R" && $81==" "{print > "outfile2"}' FS= infile
1 Like

thanks a lot! it works great :slight_smile:
i just have a quick question ... how can i extract only the first record into a new file? :slight_smile:

You could prepend:

FNR>1{exit}
1 Like