0
votes

I have two files named file1 and file2. I would like to delete the lines in file1 which are not in file2.

file1

rana     209    214 6   18  37  3   6.2
bashi    230    241 12  30  88  2.5 7.3
amir     245    250 6   14  29  2.3 4.8
joswa    190    195 6   15  45  2.5 7.5
edison   213    218 6   16  40  2.7 6.7

file2

bashi
edison

Desired output

bashi    230    241 12  30  88  2.5 7.3
edison   213    218 6   16  40  2.7 6.7

How can I do this with awk or sed?

2
While (I think) you could probably convince awk to do it, eventually, join -j 1 file1 file2 is a better choice for the purpose.John C

2 Answers

0
votes
awk 'NR==FNR{a[$1]=1;next} $1 in a {print}' file2 file1

Output:

bashi    230    241 12  30  88  2.5 7.3
edison   213    218 6   16  40  2.7 6.7

Reads file2 and creates an array indexed by first field of that, then reads file1 and if first field is in array, prints the line.

3
votes

grep is your friend:

$ grep -wFf f2 f1
bashi    230    241 12  30  88  2.5 7.3
edison   213    218 6   16  40  2.7 6.7
  • w matches words.
  • F performs fixed matching (no regex).
  • f uses another file to get the patterns.