1

I have a large tab file with 15 columns (FILE1) and a list (FILE2) of names which should appear in the table. The problem is the name may appear in columns 4 to 10 in FILE1 and it may not be a case match.

I want a command which searches line for a hit and then print the whole line. Preferably this would not be case sensitive and would not print lines where the names in FILE2 are part of a larger word.

I have tried the following:

grep -Fwf FILE2 FILE1 > out 
xargs -I {} grep "^{}" FILE1 < FILE2 > out 

the first just copies FILE1 into out. The second give a blank out file.

I've also tried a few awk commands which will either give an empty out file or as above copy FILE1. I'm trying to improve my Linux skills at the moment so if possible, if you explain your method I would be very grateful.

File1

tax_id GeneID  Symbol  LocusTag        Synonyms        dbXrefs chromosome      map_location    description     type_of_gene    Symbol_from_nomenclature_authority      Full_name_from_nomenclature_authority Nomenclature_status      Other_designations      Modification_date
7       5692769 NEWENTRY        -       -       -       -       -       Record to support submission of GeneRIFs for a gene not in Gene (Azotirhizobium caulinodans.  Use when strain, subtype, isolate, etc. is unspecified, or when different from all specified ones in Gene.).     other   -       -       -       -       20160818
9       1246500 repA1   pLeuDn_01       -       -       -       -       putative replication-associated protein protein-coding  -       -       -       -       20160813
9       1246501 repA2   pLeuDn_03       -       -       -       -       putative replication-associated protein protein-coding  -       -       -       -       20160716
9       1246502 leuA    pLeuDn_04       -       -       -       -       2-isopropylmalate synthase      protein-coding  -       -       -       -       20160903
9       1246503 leuB    pLeuDn_05       -       -       -       -       3-isopropylmalate dehydrogenase protein-coding  -       -       -       -       20150520
9       1246504 leuC    pLeuDn_06       -       -       -       -       isopropylmalate isomerase large subunit protein-coding  -       -       -       -       20160806
9       1246505 leuD    pLeuDn_07       -       -       -       -       isopropylmalate isomerase small subunit protein-coding  -       -       -       -       20160730
9       1246509 ibp     pBPS1_01        -       -       -       -       Ibp protein     protein-coding  -       -       -       -       20150801
9       1246510 repA1   pBPS1_02        -       -       -       -       repA1 protein   protein-coding  -       -       -       -       20160813

File2

sacX
arcB
metB
sprT
adrB_2
fadD
trpC
ansP2
group_1428
plsX
repA
  • show fragments of your FILE1 and FILE2 – RomanPerekhrest Apr 27 '17 at 8:20
  • 2
    please, post your inputs as text, not images. cat file1 , then select the output, copy-paste – RomanPerekhrest Apr 27 '17 at 8:43
  • 2
    Yes, posting images of text is basically useless. We need to be able to copy what you give us to our machines and test our solutions. So, please edit your question, and paste the data you show in the images as text. Also show us the output you would want to see from that data, that way we're sure we'll give you useful solutions. – terdon Apr 27 '17 at 8:50
  • The example above is just a snapshot. File1 contains 1104158 rows. – AudileF Apr 27 '17 at 10:19
  • 1
    The grep solution works here for your example. It doesn't give output because your sample doesn't have matches. If you want to have the repA1 and repA2 lines matched, leave away the w option. If you want to match case insensitive, add i option. – Philippos Apr 27 '17 at 12:18
1

Answer in comments above see @Philippos and @George Vasiliou replies

Briefly the answer is grep -Fwf FILE2 FILE1 > out

I was having an issue whereby then I executed the command it would copy FILE1. This was occurring because of trailing blank lines in FILE2. When I removed these the command worked perfectly.

As some of my text in the files may not match case-wise I included -i in the above command.

Thanks to all who helped.

  • This will fail (return false positives) if the description field has something like similar to metB in it. – Kusalananda Apr 28 '17 at 8:31
  • I don't quite get your meaning? If the command find hits in the description column rather than symbol I want those too, as not all the gene names are present in the symbol column. This would still be valid to me along as it does not report textmetBtest, – AudileF Apr 28 '17 at 8:41
  • In that case, it's fine. – Kusalananda Apr 28 '17 at 8:42

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.