#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    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
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    225
    Rep Power
    41
    Not sure if this is what you want to do, but this will print the section between the beginning match(LLL SSS) and ending match(AAA RRR):
    Code:
    $ cat f3
    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
    
    
    # print section of file between two regular expressions (inclusive)
    $ sed -n '/LLL SSS/,/AAA RRR/p' f3   # case sensitive
    LLL SSS A B C D
    N
    DDDD TTT EFG
    BBB AAA RRR J K
    LLL SSS E H G H
    N
    DDDD TTT EFG
    UUUUU BBB AAA RRR J K

IMN logo majestic logo threadwatch logo seochat tools logo