Search from one file and print the next line

Hi,

I have a file that contain more than a 1000 entries like this in one file

P400000278
P400000446
P400000659
P400000789

I want to search in file that looks like this

>P400000278 Adenosine 3'-phospho 5'-phosphosulfate transporter
MVNPWKDYVKLSTVLMGSHGLTKGSLAFLNYPAQIMFKSAKVLPVMVMGAFVPGL
>P400000789 Vacuolar iron family transporter
MAETNSNGGFRAPLLRSLDGELEKGKGKNGRPKEPWKGEVVKSIVYAGLDAIVT
>P400000005 Vacuolar iron family transporter
MAETNSNGGFRAPLLRSLDGELEKGKGKNGRPKEPWKGEVVKSIVYAGLDAIVTSFSLII 

I want output as following

>P400000278 Adenosine 3'-phospho 5'-phosphosulfate transporter
MVNPWKDYVKLSTVLMGSHGLTKGSLAFLNYPAQIMFKSAKVLPVMVMGAFV
>P400000789 Vacuolar iron family transporter
MAETNSNGGFRAPLLRSLDGELEKGKGKNGRPKEPWKGEVVKSIVYAGLDAIVT

Meaning that I want to print the next line as well as the line matching the pattern.

Can anyone help me with this?
Thanks in advance

Ashfaq

grep -A1 -f file_containing_pattern file_to_be_searched
1 Like

Thank you very much, it worked.
How can I count how many patterns matched?

grep -c -f file_containing_pattern file_to_be_searched
1 Like

Only GNU version of grep supports the -A option.

You can do something like that :

awk '
    NR==FNR { pattern[">" $1] ; next }
    $1 in pattern { print; print_next=1; next }
    print_next-- > 0
' file_containing_pattern file_to_be_searched

Jean-Pierre.

1 Like

if f1 is the name of the file containing the pattern
and f2 is the file containing the data :

awk 'NR==FNR{a[$0];next}($1 in a){printf ">" $0}' f1 RS='>' f2
1 Like