Tuesday, May 27, 2008

Print first or last occurrence of pattern range - awk


As you know:

We can print section of file between two regular expressions (inclusive)

by


$ awk '/firstpattern/,/secondpattern/' file


Now if I have to print the first or last occurrence of the pattern range, here is the way:

Input File:


$ cat myfile5.txt
This is a test file to print the last and first occurrence of the text between two patterns

{ BASH
SH }

Dummy line 1
{ BASH
SH
}

New to Bash Scripting ?
{
BASH
SH
}

{
BASH
SH }

Ok, about to complete

{ BASH SH }

End of the test file. Try your code now to print the same.



#Printing first occurrence of the text(s) between two patterns ({ and })

$ awk '/{/ {a=""}
#Printing first occurrence
/{/,/}/ {a=(a=="") ? $0 : a RS $0}
/}/ {exit}
END {
print a
}' myfile5.txt


Output:

{ BASH
SH }



#Printing last occurrence of the text(s) between two patterns ({ and })

$ awk '/{/ {r=""}
#Printing last occurrence
/{/,/}/ {r=(r=="") ? $0 : r RS $0}
END {
print r
}' myfile5.txt


Output:

{ BASH SH }

2 comments:

Narayana said...

Hi, I am new to linux and have a challenge while I am debugging my application logs n linux boxes.

our log file xxx.log will have different responses coming in its way while an user logs in. Each response might be of 2000 lines or more. Many users do login at a time and our log file goes big and big.

What command or script should I write to take last occurrence of particular response of one user. In the following example what should I do to take last occurrence of response2 of user1 ?

I am not interested in running less command and copy from the screen. could you guys suggest me with a command or script ?

Responses in logs are like below:


1000 lines or more data
user1
1000 lines or more data



1000 lines or more data
user1
1000 lines or more data



1000 lines or more data
user2
1000 lines or more data



1000 lines or more data
user1
1000 lines or more data



1000 lines or more data
user2
1000 lines or more data



1000 lines or more data
user3
1000 lines or more data


so on ...


Thanks in advance,
Narayana.V

Narayana said...

Hi, I am new to linux and have a challenge while I am debugging my application logs n linux boxes.

our log file xxx.log will have different responses coming in its way while an user logs in. Each response might be of 2000 lines or more. Many users do login at a time and our log file goes big and big.

What command or script should I write to take last occurrence of particular response of one user. In the following example what should I do to take last occurrence of response2 of user1 ?

I am not interested in running less command and copy from the screen. could you guys suggest me with a command or script ?

Responses in logs are like below:


1000 lines or more data
user1
1000 lines or more data



1000 lines or more data
user1
1000 lines or more data



1000 lines or more data
user2
1000 lines or more data



1000 lines or more data
user1
1000 lines or more data



1000 lines or more data
user2
1000 lines or more data



1000 lines or more data
user3
1000 lines or more data


so on ...


Thanks in advance,
Narayana.V

© Jadu Saikia www.UNIXCL.com