Reputation: 733
I have a file #1 that has lines as such:
SSJ4_00089026.dpx
SSJ4_00089134.dpx
SSJ4_00090253.dpx
SSJ4_00090267.dpx
SSJ4_00090379.dpx
SSJ4_00090392.dpx
SSJ4_00090492.dpx
SSJ4_00094320.dpx
and another file #2 that has lines as such:
693d092aba91a6acd2b8d73903fe4f78 SSJ4_00089026.dpx
0333b53c486de6a7214c05fd583f9745 SSJ4_00089134.dpx
fdc713709251e0538a84549355627e65 SSJ4_00090253.dpx
a8766ba5d6e1ee854bf7db10c05c7e55 SSJ4_00090267.dpx
733f6564ca442974555a76b91d506cff SSJ4_00090379.dpx
f6154ddf1d7ae7c8076f08c25c9fe2c3 SSJ4_00094320.dpx
All of the lines in #1 are in #2, but they don't have the hash. Not all of the lines in #2 are in #1.
I want to pull all the lines that are #1 from #2 including the hash and put them into a new file, #3. So that #3 would look like:
ccfd092aba91a6acd2b8d73903fe4f78 SSJ4_00089026.dpx
056nb53c486de6a7214c05fd583f9745 SSJ4_00089134.dpx
45g313709251e0538a84549355627e65 SSJ4_00090253.dpx
etc
I'm thinking some command with grep such as
grep -v -f -i file#1 file#2 >> file#3
or maybe awk
Upvotes: 2
Views: 3838
Reputation: 37268
grep -iFf file1 file2 > file
you need to tell grep
that it is in fgrep
mode with the -F
option, then the -f
specifies what file to read from.
Note that I have changed your >>
redirect (append) to >
(create).
You'll trip yourself up in testing using >>
, as your first tests will always appear at the top of the file, and if you're rushing you won't think it is working. Use >
for development and if you really need append mode, then add it after you are certain you basic cmd is working as required.
Finally, I'd use the -i
(ignore case) option sparingly. If you really need to match lower case versions of your target strings, better to include those in your file1
, so your process is self documenting.
IHTH
Upvotes: 4
Reputation: 965
NOTE: It was pointed out in the comments correctly that grep
will have problems if you stick to having fields.
do this grep -Fwf file2 file1 > out
.
,or use
awk 'NR==FNR{pats[$0]; next} $2 in pats' File2 File1
Upvotes: 4
Reputation: 2025
cat file1 | xargs -I@ -n1 grep @ file2 | tee file3
Print file1. Using xargs make each line of file 1 the pattern to grep call. Write the output to file3
Input file1:
SSJ4_00086400.dpx
SSJ4_00086403.dpx
Input file2:
693d092aba91a6acd2b8d73903fe4f78 SSJ4_00086400.dpx
0333b53c486de6a7214c05fd583f9745 SSJ4_00086401.dpx
fdc713709251e0538a84549355627e65 SSJ4_00086402.dpx
a8766ba5d6e1ee854bf7db10c05c7e55 SSJ4_00086403.dpx
733f6564ca442974555a76b91d506cff SSJ4_00086404.dpx
f6154ddf1d7ae7c8076f08c25c9fe2c3 SSJ4_00086405.dpx
Output file3:
693d092aba91a6acd2b8d73903fe4f78 SSJ4_00086400.dpx
a8766ba5d6e1ee854bf7db10c05c7e55 SSJ4_00086403.dpx
Upvotes: 1