0

I have two files: one containing a list of IDs (file A) and other containing a list of IDs plus their corresponding text -which is always on the next line- (file B).

File A:

>161@1983
>947@2009
>59@2002
>5933@2005

File B:

>161@1983
text
>8899@2007
more text
>6000@2017
more and more text
>5933@2005
idk
>31@2004
you get the idea
etc...

How can I extract only the IDs + their corresponding text from file B that match the IDs from file A?

Expected output:

>161@1983
text
>5933@2005
idk

Thanks

2

With GNU grep, FreeBSD grep:

grep -A1 -Ff fileA fileB
  • Tried it on my files and didn't work.. Gives me a blank output. Is there any other way? – Manuel May 7 '18 at 3:48
  • @Manuel does fileA contains id the same string as id in fileB? -F causes the id line in fileA matches the id line in fileB, if it's substring, or pattern, then it doesn't match – cuonglm May 7 '18 at 3:51
  • FileA's and B's IDs are exact same strings. Not substrings nor patterns. – Manuel May 7 '18 at 4:02
  • @Manuel So checking if you have trailing whitespaces? I tested with your input and it give me desired output. Also make sure fileA is the one contains list of ids, and the fileA fileB order in command line is a must. – cuonglm May 7 '18 at 4:06
  • Yeah, I also tried with the example I provided and it worked. Weird. Already checked for whitespaces and everything is ok. FileA contains 535 IDs and fileB contains 3736 IDs + 3736 lines of text, one after each ID. Can size be an issue when using your command? – Manuel May 7 '18 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.