Match Pattern after certain pattern and Print words next to Pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Match Pattern after certain pattern and Print words next to Pattern
# 1  
Old 05-28-2013
Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right.
I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition.. iez..
Though before LLL SSS I have TTT , I need to print only the ones that comes after LLL SSS to AAA RRR ,and again when I encounter LLL SSS need to do the same , I have tried declaring a awk flag variable, but it doesn’t seem to work. Kindly help me.


Code:
var1="LLL";var2="SSS";var3="TTT";var4="AAA";var5="RRR";
awk -v flag=1 '{
for (m=1;m<=NF;m++)
if($m~m/'$var1'/ && $(m+1)~/'$var2'/)
print "ABCD: ", $(m+2),$(m+3),$(m+4)
{flag=0};
else {
if {flag==0 && $m~/'var3'/)
print "EFG : ", $(m+1);
else { 
if (flag==0 && $m~/'$var4'/ && $(m+1)~/'var5'/)
print "HIJ: ",$(m+2),$(m+3) 
{flag=1}; 
}
}
}
}' flineame




My file is something like this:

Code:
AAA RRR H I
DDDD OOO HI J UGC RR
TTT YYY
LLLLL IIIIII
LLL SSS A B C D
N
DDDD TTT EFG
BBB AAA RRR J K
HHHHHH RRRRRR 
LLL SSS E H G H
N
DDDD TTT EFG
UUUUU BBB AAA RRR J K


Last edited by Scott; 05-28-2013 at 03:32 AM.. Reason: Added code tags; removed formatting
# 2  
Old 05-28-2013
Quote:
Originally Posted by 100bees
I need to print only the ones that comes after LLL SSS to AAA RRR ,and again when I encounter LLL SSS need to do the same
Here is an awk approach:
Code:
awk '
        {
                for ( i = 1; i <= NF; i++ )
                {
                        if ( $i == "LLL" && $( i + 1 ) == "SSS" )
                        {
                                F = 1
                                i += 2
                        }
                        if ( $i == "AAA" && $( i + 1 ) == "RRR" )
                        {
                                F = 0
                        }
                        if ( F )
                                s = s ? s OFS $i : $i
                }
                if ( s )
                {
                        print s
                        s = ""
                }
        }
' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 06-04-2013
Thanks, using your approach , i could achieve the desired result.I have set flags for each condition and iterated it to next field.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

4. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

5. Shell Programming and Scripting

Print only next pattern in a line after a pattern match

I have 2013-06-11 23:55:14 1Umexd-0004cm-IG <= user@domain.com I need sed/awk operation on this, so that it should print the very next pattern only after the the pattern mach <= ie only print user@domain.com (7 Replies)
Discussion started by: anil510
7 Replies

6. Shell Programming and Scripting

Script to match a pattern and print only the pattern and after that

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ...... .......xyz: abz: ..... I have tried using awk and cut, bu the position of these values keep changing, so I can use awk and split it into columns. ... (14 Replies)
Discussion started by: Serena
14 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, file.tab: Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

9. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

10. UNIX for Advanced & Expert Users

I am trying to find pattern between two words but unable to get that pattern..

HI.... It's fallow up file .. #./show.sh click enter button.. i am gettng the fallowup file. its keep on running every time why because there are lots of users working on it. In that file i want to search pattern between two words for ex: SELECT DISTINCT... (7 Replies)
Discussion started by: ksr.test
7 Replies
Login or Register to Ask a Question