Gui O
Gui O

Reputation: 383

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 ?

Upvotes: 0

Views: 53

Answers (3)

Brian Agnew
Brian Agnew

Reputation: 272437

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.

Upvotes: 1

Josh Jolly
Josh Jolly

Reputation: 11796

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.

Upvotes: 1

fedorqui
fedorqui

Reputation: 290525

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.

Upvotes: 3

Related Questions