Search from one file and print the next line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search from one file and print the next line
# 1  
Old 02-28-2012
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
Code:
P400000278
P400000446
P400000659
P400000789

I want to search in file that looks like this
Code:
>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
Code:
>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

Last edited by Franklin52; 02-28-2012 at 09:10 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-28-2012
Code:
grep -A1 -f file_containing_pattern file_to_be_searched

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 02-28-2012
Thank you very much, it worked.
How can I count how many patterns matched?
# 4  
Old 02-28-2012
Code:
grep -c -f file_containing_pattern file_to_be_searched

This User Gave Thanks to balajesuri For This Post:
# 5  
Old 02-28-2012
Quote:
Code:
grep -A1 -f file_containing_pattern file_to_be_searched

Only GNU version of grep supports the -A option.

You can do something like that :
Code:
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.
This User Gave Thanks to aigles For This Post:
# 6  
Old 02-28-2012
if f1 is the name of the file containing the pattern
and f2 is the file containing the data :

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

This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search file and print everything except multiple search terms

I'm trying to find a way to search a range of similar words in a file. I tried using sed but can't get it right:sed 's/\(ca01\)*//'It only removes "ca01" but leaves the rest of the word. I still want the rest of the information on the lines just not these specific words listed below. Any... (3 Replies)
Discussion started by: seekryts15
3 Replies

2. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

3. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

4. Shell Programming and Scripting

Search words in a line and print next 15 lines.

I have a text file ( basically a log file) and i have 2 words (alpha, beta), Now i want to search these two words in one line and then print next 15 lines in a temp file. there would be many lines with alpha and beta But I need only last occurrence with "alpha" and "beta" and next 15 lines. ... (4 Replies)
Discussion started by: kashif.live
4 Replies

5. Shell Programming and Scripting

Search Duplicates, Print Line #

Masters, I have a text file in the following format. vrsonlviee RVEBAALSKE lyolzteglx UUOSIWMDLR pcybtapfee DKGFJBHBJO ozhrucfeau YQXATYMGJD cjwvjolrcv YDHALRYQTG mdukphspbc CQZRIOWEUB nbiqomzsgw DYSUBQSSPZ xovgvkneav HJFQQYBLAF boyyzdmzka BVTVUDHSCR vrsonlviee TGTKUCUYMA... (2 Replies)
Discussion started by: genehunter
2 Replies

6. Shell Programming and Scripting

Search a string and print the rest of line

Hi Guys, I need to search a string and print the rest of the lines... input: 8 0 90 1 0 59 20 2488 96 30006dde372 S ? 0:00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n output: 00 /etc/opt/SUNWconn/atm/bin/atmsnmpd -n Actually i don even need the first "00".. any suggestions is appreciated..... (13 Replies)
Discussion started by: mac4rfree
13 Replies

7. Shell Programming and Scripting

awk search column, print line

Hello. I've been banging my head against walls trying to search a comma delimited file, using awk. I'm trying to search a "column" for a specific parameter, if it matches, then I'd like to print the whole line. I've read in multiple texts: awk -F, '{ if ($4 == "string") print $0 }'... (2 Replies)
Discussion started by: Matthias03
2 Replies

8. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

9. Shell Programming and Scripting

Print the line within the search pattern

Hi Guys, I had file as typedef struct { char TrailerType1; char TrailerTxt1; }Trailer; typedef struct { char PfigMoneyType; char PfigMoneyvalue; }PfigMoney; i need to print the lines within the search pattern. if i give the search pattern as... (3 Replies)
Discussion started by: manosubsulo
3 Replies

10. Shell Programming and Scripting

Search for text and print the next line

Hi, I want to write a small script to search for a text in file and when its found I want to print the next line. I try to write that script but I could not manage it, I just write the following script the find the exact line but I want the next line. $ sed -n -e '/Form not/p' test.txt... (2 Replies)
Discussion started by: alijassim
2 Replies
Login or Register to Ask a Question