简体   繁体   中英

Bash grep in file which is in another file

I have 2 files, one contains this : file1.txt

632121S0 126.78.202.250 1
131145S0 126.178.20.250 1

the other contain this : file2.txt

632121S0        126.78.202.250  OBS
131145S0        126.178.20.250  OBS
313359S2        126.137.37.250  OBS

I want to end up with a third file which contains :

632121S0        126.78.202.250  OBS
131145S0        126.178.20.250  OBS

Only the lines which start by the same string in both files. I can't remember how to do it. I tried several grep, egrep and find, i still cannot use it properly... Can you help please ?

You can use this awk:

$ awk 'FNR==NR {a[$1]; next} $1 in a' f1 f2
632121S0        126.78.202.250  OBS
131145S0        126.178.20.250  OBS

It is based on the idea of two file processing , by looping through files as this:

  • first loop through first file, storing the first field in the array a .
  • then loop through second file, checking if its first field is in the array a . If that is true, the line is printed.

To do this with grep, you need to use a process substitution :

grep -f <(cut -d' ' -f1 file1.txt) file2.txt

grep -f uses a file as a list of patterns to search for within file2. In this case, instead of passing file1 unaltered, process substitution is used to output only the first column of the file.

If you have a lot of these lines, then the utility join would likely be useful.

join - join lines of two files on a common field

Here's a set of examples .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM