0

I am trying to find strings from one file in another file and I need to have the search string printed once, and the lines for matches following.

The strings I want to find matches for are in a file with 2 lines per record, no empty lines etc. Line 1 contains the name, line 2 the string I need to find in the second file. sed -n '1~2p' will print all the names, but I will need to print only the first name, pass the value of the second line to grep, execute the grep command, then print the second record's name (line 3), pass line 4 to grep and so on. An alternative would be to use a while or until loop with a counter incremented by 2, but then I don't know how to pass the value of counter to sed.

  • Please edit and add an example with input and desired output using the formatting tools. – dessert Nov 6 '17 at 22:16
  • I apologize, I overlooked your comment. I have only two files. the first is miRNA.fasta: >vvi-miR390 AAGCTCAGGAGGGATAGCGCC >vvi-miR408 ATGCACTGCCTCTTCCCTGGC >vvi-miR169d CAGCCAAGAATGATTTGCCGG The second is similar, RNAseqReads.fasta, but with much longer strings. output in new comment – Susanne Nov 7 '17 at 15:09
  • output I am thinking about would be: >vvi-miR390 >RNAseq Read Name 1 CTTTGCCAGGACTTTGGTTCCTGAAAGGTAGATGTCCACGTGGCAAATTGCTGCTGCCAC A -- >RNAseq Read Name 2 AAACAATACTTTGATCCACTTGCTAATTCAAAGCCTTGAAGAAATCAAAGAGCTCCGCGC -- >vvi-miR408 MIMAT0005733 Vitis vinifera miR408 >RNAseq Read Name 3 CCACTTCTGTTGGAGTGGTAGTAGCCCGTTGTGAGACAACGCCACGTACCATTTTCAGT TTTCCTAGCCCAAATCCCCATGAACAGAACTCCCACGGTCAATAAGTTTCAACAGGATCC – Susanne Nov 7 '17 at 15:09
  • 1
    Please always add additional information directly to your question by editing it rather than commenting! That's why I linked you the formatting tools… – dessert Nov 7 '17 at 15:16
2

If I understand you correctly, then given

$ head File\ {1..3}
==> File 1 <==
foo
Pattern 1
bar

==> File 2 <==
foo
bar
baz

==> File 3 <==
Pattern 3
foo
baz

and

$ cat infile
File 1
Pattern 1
File 2
Pattern 2
File 3
Pattern 3

then

$ sed -E '$!N; s/(.*)\n(.*)/grep -H -- "\2" "\1"/e' infile
File 1:Pattern 1

File 3:Pattern 3

or

$ while read -r file; do 
    if read -r pattern; then grep -H -- "$pattern" "$file"; fi
  done < infile 
File 1:Pattern 1
File 3:Pattern 3
| improve this answer | |

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.