Awk to extract patterns till it hits blank line (in for loop)
I have a list of patterns in file1
Code:
10047134 Code:
>gi|10047134|ref| The awk one-liner I used works given a single pattern: Code:
awk 'BEGIN{RS=ORS="\n\n"; FS="\n"}/pattern/' infile Code:
for i in `cat file1` Can you suggest a better way out ?! |
Well being it is a number of varying length, I presume you are defining the pattern with pipes as delimiters?
As for reading 2 files, pass file1 to awk inside your BEGIN and assign individual numbers to an array (normally you could do this in the script part instead but your change in RS would read all of the first file), then check each number in second file for each record against the array. The alternative is the way you started and simply use the -v option to assign $i to an awk variable. Of course the hit here is the awk is executed every time. |
Putting in an array and matching records gives me the line containing the pattern.
How do i go about printing the lines below it? :| |
Quote:
Code:
awk -F"|" 'FNR == NR { pattern[$1]++; next } FNR < NR { if ( $2 in pattern ) { while ( $0 !~ /^$/ ) { print; getline } print "" } }' file1 file2 |
Another alternative:
Code:
awk 'BEGIN{RS = ""; ORS = "\n\n"; FS = "\n"}FNR == NR{while(++i <= NF)a[$i]++;FS="|";next}$2 in a' file1 file2 |
Got it...!!! :D
Thanks a ton grail n colucix :) |
All times are GMT -5. The time now is 03:24 PM. |