priyank
priyank

Reputation: 4724

grep "output of cat command - every line" in a different file

Sorry title of this question is little confusing but I couldnt think of anything else. I am trying to do something like this

cat fileA.txt | grep `awk '{print $1}'` fileB.txt

fileA contains 100 lines while fileB contains 100 million lines.

What I want is get id from fileA, grep that id in a different file-fileB and print that line.

e.g fileA.txt
1234
1233

e.g.fileB.txt
1234|asdf|2012-12-12
5555|asdd|2012-11-12
1233|fvdf|2012-12-11

Expected output is

1234|asdf|2012-12-12
1233|fvdf|2012-12-11

Upvotes: 3

Views: 12032

Answers (3)

Kent
Kent

Reputation: 195039

awk alone can do that job well:

awk -F'|' 'NR==FNR{a[$0];next;}$1 in a' fileA fileB

see the test:

kent$  head a b
==> a <==
1234
1233

==> b <==
1234|asdf|2012-12-12
5555|asdd|2012-11-12
1233|fvdf|2012-12-11

kent$  awk -F'|' 'NR==FNR{a[$0];next;}$1 in a' a b
1234|asdf|2012-12-12
1233|fvdf|2012-12-11

EDIT

add explanation:

-F'|'  #| as field separator (fileA)
'NR==FNR{a[$0];next;} #save lines in fileA in array a
 $1 in a  #if $1(the 1st field) in fileB in array a, print the current line from FileB

for further details I cannot explain here, sorry. for example how awk handle two files, what is NR and what is FNR.. I suggest that try this awk line in case the accepted answer didn't work for you. If you want to dig a little bit deeper, read some awk tutorials.

Upvotes: 4

squiguy
squiguy

Reputation: 33370

If the id's are on distinct lines you could use the -f option in grep as such:

cut -d "|" -f1 < fileB.txt | grep -F -f fileA.txt

The cut command will ensure that only the first field is searched for in the pattern searching using grep.

From the man page:

-f FILE, --file=FILE
Obtain patterns from FILE, one per line.  
The empty file contains zero patterns, and therefore matches nothing.
(-f is specified by POSIX.)

Upvotes: 1

Anton Kovalenko
Anton Kovalenko

Reputation: 21507

Getting rid of cat and awk altogether:

grep -f fileA.txt fileB.txt

Upvotes: 11

Related Questions