1

I have a file A with one colum with a list of strings like this:

12
123
1234
1234567

I want to use the strings in file A to grep the lines that contains them in file B, and file B looks like this:

1       0/0     ./.     0/1     0/0
12      0/0     0/0     1/1     ./.
1234    1/1     0/1     ./.     0/0
12345   0/0     0/0     1/1     1/1
123456  1/1     1/1     ./.     ./.

In this case, I´m waiting an output that match EXACTLY the strings that are in file A, that looks like this:

12      0/0     0/0     1/1     ./.
1234    1/1     0/1     ./.     0/0

I've used grep -wf A B and it works perfectly, but the problem is that my real files are very heavies and the proces is very costly. Someone have any different idea to get the same results but using other command line?

Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
user2820398
  • 63
  • 1
  • 1
  • 5

1 Answers1

2

You can use this awk as an alternative:

awk 'NR==FNR{a[$1]; next} $1 in a' file1 file2
12      0/0     0/0     1/1     ./.
1234    1/1     0/1     ./.     0/0

Explanation:

NR == FNR {                  # While processing the first file
  a[$1]                      # store the 1st field in array a
  next                       # move to next line
}
$1 in a                      # while processing the second file
                             # if 1st field is in array then print it
anubhava
  • 761,203
  • 64
  • 569
  • 643