extract rows that have a specific name

Hi I want to extract rows in a large files that have a specific name.

The name can be "starved and rich", "rich", "starved" or " ".

Heres an example:

bob starved and rich
jack rich
joey starved 
mike         

so it can have either 3 names or no name. I want to put the names into a separate file and the no names into another.

output1:

bob starved and rich
jack rich
joey starved 

output2:

mike        

thanks

So save names in file one if it has names more than 1 name and and save to another file if it is more than 1?

Brute force method:

awk -v nf=name.file ' /^[^ \t]*[ \t]+starved and rich/ || /^[^ \t]*[ \t]+starved/ || /^[^ \t]*[ \t]+rich/  {print >nf; next;} NF == 1 ' input-file >nonames.file
1 Like
awk '{ if(NF > 1) print;}' data_file.txt >> file1.txt
awk '{ if(NF == 1) print;}' data_file.txt >> file2.txt

file1.txt contains
bob starved and rich
jack rich
joey starved

file2.txt contains
mike

or if you want to do it in 1 line

awk '{ if(NF > 1) {print >> "file1.txt";} if(NF == 1) {print >> "file2.txt";}}' data_file.txt
grep -E  "^[^ ]* (starved and rich|rich|starved)$" infile > output1
grep -Ev "^[^ ]* (starved and rich|rich|starved)$" infile > output2
1 Like