Home > Software engineering >  How to use awk or sed to split a file into 2 parts based on a list of value?
How to use awk or sed to split a file into 2 parts based on a list of value?

Time:09-23

I have two file. One of them is the main file which contains a lot of columns. Another one contains the information about a list of samples. Now I would like to split the main file into 2 part based on which the sample (row) is in the list of second file or not. Now I use the code like this

awk 'NR == FNR {a[$1]; next} !($1 in a)' $i.list $i > no_in_list.$i

to exclude the sample which in the list, but I was wondering if it is possible to also keep the samples which in the list.

CodePudding user response:

You could use the print >> "file" action to print in a specified file instead of the standard output (tested with GNU awk):

awk -v nil="no_in_list.$i" -v il="in_list.$i" '
  NR == FNR {a[$1]; next}
  !($1 in a) {print >> nil}
  ($1 in a) {print >> il}' $i.list $i
  • Related