Match 2 different patterns and print the lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match 2 different patterns and print the lines
# 1  
Old 05-29-2014
Match 2 different patterns and print the lines

Hi,

i have been trying to extract multiple lines based on two different patterns as below:-

file1
Code:
@jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla
sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk
fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf
jahfjkhflkhalfdhfwearhahfl
@jkm|sdf|wud08q|168.2.1.3 bkblblblabdlablfbalflaflabfblaslfbalfblaf
gfrkqghfrkadegfeowfrkefewurfgefhjwavfss
gjgferjwgfrjewgfjrewgafrjgjwgrfjwgerjwg
we
@jkm|qpw|dgs901|192.2.45.3 blabslabdsa
ahfkghqewafroewforewfthewofghrwgh
whrgfrjwgflwgjh
@jkm|ppl|hfjsf87|192.2.3.8 bdfksfbdkbfdfbdgksdgbks
ksgfhglkshddlgsslghdlgh;lsdgj;sdkgs

I need to extract the ip address, for example: 192.2.3 and print all the lines start from "@" until it reaches the next "@" like below:-

output
Code:
@jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla
sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk
fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf
jahfjkhflkhalfdhfwearhahfl
@jkm|ppl|hfjsf87|192.2.3.8 bdfksfbdkbfdfbdgksdgbks
ksgfhglkshddlgsslghdlgh;lsdgj;sdkgs

for each line in bold (first line of each record), there is \n at the end.

the codes that i tried:-

Code:
awk '/^@/{p=0} /^@jkm/{p=1}p' file1

and tried to find and match 192.2.3. by grep and etc but did not get the right output. Can anyone tell me how to run two awk commands for implementing 2 different things one after another such as a case like this? I tried some that i found but none is successful. I am still trying my best to learn and understand awk and sed. Thanks
# 2  
Old 05-29-2014
Code:
$ awk '/^@/{s = ($0~/192\.2\.3/) ? 1 : 0}s'  file

@jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla
sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk
fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf
jahfjkhflkhalfdhfwearhahfl
@jkm|ppl|hfjsf87|192.2.3.8 bdfksfbdkbfdfbdgksdgbks
ksgfhglkshddlgsslghdlgh;lsdgj;sdkgs


Last edited by Akshay Hegde; 05-29-2014 at 01:37 AM..
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 05-29-2014
Hi akshay Hegde,

i dont have gawk. Smilie
# 4  
Old 05-29-2014
Sorry I misread, Check I just modified my post, use nawk if you are on sunos / solaris

---------- Post updated at 11:16 AM ---------- Previous update was at 11:08 AM ----------

use this if you want to replace using shell variable

Code:
$ awk '/^@/{s = ($0~ip) ? 1 : 0}s' ip="192.2.3" file

Code:
$ ip="192.2.3"
$ awk '/^@/{s = ($0~ip) ? 1 : 0}s' ip="$ip" file

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 05-29-2014
Quote:
Originally Posted by Akshay Hegde
Code:
$ awk '/^@/{s = ($0~/192\.2\.3/) ? 1 : 0}s'  file

@jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla
sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk
fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf
jahfjkhflkhalfdhfwearhahfl
@jkm|ppl|hfjsf87|192.2.3.8 bdfksfbdkbfdfbdgksdgbks
ksgfhglkshddlgsslghdlgh;lsdgj;sdkgs


Your codes work perfectly. Thanks so much. If u don't mind, can u explain briefly about your codes? Smilie thanks
# 6  
Old 05-29-2014
... on a side note, the ternary isn't necessary:
Code:
awk '/^@/{s=/192\.2\.3/}s' file

This User Gave Thanks to jethrow For This Post:
# 7  
Old 05-29-2014
Quote:
Originally Posted by jethrow
... on a side note, the ternary isn't necessary:
Code:
awk '/^@/{s=/192\.2\.3/}s' file

It is a simple code and works great. Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Egrep patterns in a file and limit number of matches to print for each pattern match

Hi I need to egrep patterns in a file and limit number of matches to print for each matched pattern. -m10 option is not working out in my sun solaris 5.10 Please guide me the options to achieve. if i do head -10 , i wont be getting all pattern match results as output since for a... (10 Replies)
Discussion started by: ananan
10 Replies

4. Shell Programming and Scripting

Print all lines between patterns

Hi Gurus, I have a requirement where I need to display all lines between 2 patterns except the line where the first pattern in it. I tried the following command using awk but it is printing all lines except the lines where the 2 patterns exist. awk '/TRANSF_/{ P=1; next } /Busy/ {exit} P'... (9 Replies)
Discussion started by: svajhala
9 Replies

5. Shell Programming and Scripting

How to print only lines in between patterns?

Hi, I want to print only lines (green-italic lines) in between first and last strings in column 9. there are different number of lines between each strings. 10 AUGUSTUS exon 4558 4669 . - . 10.g1 10 AUGUSTUS exon 8771 8889 . ... (6 Replies)
Discussion started by: jamo
6 Replies

6. Shell Programming and Scripting

Need to print between patterns AND a few lines before

I need to print out sections (varying numbers of lines) of a file between patterns. That alone is easy enough: sed -n '/START/,/STOP/' I also need the 3 lines BEFORE the start pattern. That alone is easy enough: grep -B3 START But I can't seem to combine the two so that I get everything between the... (2 Replies)
Discussion started by: Finja
2 Replies

7. Shell Programming and Scripting

Retrieve lines that match any occurence in a list of patterns

I have two files. The first containing a header and six columns of data. Example file 1: Number SNP ID dbSNP RS ID Chromosome Result_Call Physical Position 787066 SNP_A-8575395 RS6650104 1 NOCALL 564477 786872 SNP_A-8575125 RS10458597 1 AA ... (13 Replies)
Discussion started by: Selftaught
13 Replies

8. Shell Programming and Scripting

Match multiple patterns in a file and then print their respective next line

Dear all, I need to search multiple patterns and then I need to print their respective next lines. For an example, in the below table, I will look for 3 different patterns : 1) # ATC_Codes: 2) # Generic_Name: 3) # Drug_Target_1_Gene_Name: #BEGIN_DRUGCARD DB00001 # AHFS_Codes:... (3 Replies)
Discussion started by: AshwaniSharma09
3 Replies

9. Shell Programming and Scripting

print lines which match multiple patterns

Hi, I have a text file as follows: 11:38:11.054 run1_rdseq avg_2-5 999988.0000 1024.0000 11:50:52.053 run3_rdrand 999988.0000 1135.0 128.0417 11:53:18.050 run4_wrrand avg_2-5 999988.0000 8180.5833 11:55:42.051 run4_wrrand avg_2-5 999988.0000 213.8333 11:55:06.053... (2 Replies)
Discussion started by: annazpereira
2 Replies

10. Shell Programming and Scripting

Removing file lines that each match to a different patterns

I have a very large file (10,000,000 lines), that contains a sample id and a property of that sample. I have another file that contains around 1,000,000 lines with sample ids that I want to remove from the original file (create a new file without these lines). I know how to do this in Perl, but it... (9 Replies)
Discussion started by: Jo_puzzled
9 Replies
Login or Register to Ask a Question