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

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.

其他提示

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.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top