Last occurrence of code between two tags


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Last occurrence of code between two tags
# 1  
Old 01-18-2012
Last occurrence of code between two tags

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:
Code:
<response1>
1000 lines or more data 
<userId>user1<userId1>
1000 lines or more data
</response1>

<response2>
1000 lines or more data 
<userId>user1<userId1>
1000 lines or more data
</response2>

<response1>
1000 lines or more data 
<userId>user2<userId1>
1000 lines or more data
</response1>

<response3>
 1000 lines or more data 
 <userId>user1<userId1>
 1000 lines or more data
 </response3>

<response2>
 1000 lines or more data 
 <userId>user2<userId1>
 1000 lines or more data
 </response2>

<response1>
  1000 lines or more data 
  <userId>user3<userId1>
  1000 lines or more data
  </response1>

so on ...

Thanks in advance,
Narayana.V


Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 01-18-2012 at 12:32 PM.. Reason: code tags
# 2  
Old 01-18-2012
If you have GNU awk, you could try this:
Code:
tac xxx.log  | awk '/user1/{print; exit}' RS="</*response[0-9]*>" | tac

# 3  
Old 01-18-2012
Hi Mirni, Thanks for replying !

How do I know if I have GNU awk or not ?

I am getting the following exception when I run the command that you have given me bash: tac: command not found

is there any awk or sed command that I can execute to get the work done ?

Thanks
Narayana.V
# 4  
Old 01-18-2012
You find your version of awk by typing
Code:
awk --version

What system are you on?
Try:
Code:
awk '{a[i++]=$0}END{for(j=i-1;j>=0;j--) if(a[j]~/user1/) {print a[j];exit} }' RS="</*response[0-9]*>" xxx.log

# 5  
Old 01-19-2012
Thanks again Mirni !

I am working on Solaris.
awk --version is not working to get the version.

your awk code ran and given me some random one line output from the log, it has not given me the output that I required. This line is not part of the response also.

do you have anything to suggest ?
# 6  
Old 01-19-2012
On Solaris it is best to use /usr/xpg4/bin/awk, but that is not gawk (GNU awk)..

See if this works:
Code:
/usr/xpg4/bin/awk '/<userId>user1<userId1>/&&/<response2>/{p=$0}END{print p}' RS= infile

-or-
Code:
/usr/xpg4/bin/awk '/user1/&&/response2/{p=$0}END{print p}' RS= infile


Last edited by Scrutinizer; 01-19-2012 at 11:26 AM..
# 7  
Old 01-19-2012
Thank You Scrutinizer !
First command is having syntax error.

Second command worked partially as it has not printed the complete response2 of user1 where it missed to have part of response below and also it printed some part of response1 of some other user on top ..

It has given me the output like below


some 100 lines from response1
<response2>
got till
<userId>user1</userId>
-- some lines from response2
-- missed most of the lines here ..

can you pls suggest any changes ?

Thanks
Narayana.V
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. UNIX for Dummies Questions & Answers

Code for exact match to count occurrence

Hi all, I have an input file as below. I would like to count the occurrence of pattern matching 8th field for each line. Input: field_01 field_02 field_03 field_04 field_05 field_06 field_07 field_08 TA T TA T TA TA TA... (3 Replies)
Discussion started by: huiyee1
3 Replies

3. Shell Programming and Scripting

Substitute first occurrence of keyword if occurrence between two other keywords

Assume a string that contains one or multiple occurrences of three different keywords (abbreviated as "kw"). I would like to replace kw2 with some other string, say "qux". Specifically, I would like to replace that occurrence of kw2 that is the first one that is preceded by kw1 somewhere in the... (4 Replies)
Discussion started by: M Gruenstaeudl
4 Replies

4. Shell Programming and Scripting

number of occurrence

: i need a bash script to convert the displayed output 12 14 15 12 15 13 to 12 * 2 ,13 * 1,14*1,15*1 Thanks, nevil (2 Replies)
Discussion started by: nevil
2 Replies

5. Shell Programming and Scripting

Replace second occurrence only

HPUX /bin/sh (posix) I have a file as such cat dog mouse deer elk rabbit mouse rat pig I would like to replace the second occurrence of mouse in this file with mouse2. The rest of the file has to stay exactly as is. I'm not sure exactly where mouse might be (could be first,second,third... (5 Replies)
Discussion started by: lyoncc
5 Replies
Login or Register to Ask a Question