The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
print last matched pattern using perl er_ashu Shell Programming and Scripting 5 07-23-2008 03:18 AM
Replacing a word after a matched pattern maxmave Shell Programming and Scripting 1 06-12-2008 04:54 PM
Count of matched pattern occurance palash2k UNIX for Dummies Questions & Answers 3 04-24-2008 04:33 PM
Multile Pattern Search in a same line and delete sasree76 Shell Programming and Scripting 2 04-16-2008 03:12 PM
Perl onliner to search the last line with an occurence of a pattern ammu Shell Programming and Scripting 4 01-31-2008 01:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-31-2008
Sharmila_P Sharmila_P is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 19
search for the matched pattern by tracing back from the line

Hi,

I want to grep the line which has 'data11'.then from that line, i need to trace back and find out the immediate line which has the same timestamp of that grepped line.
for eg:
log file:
-----------
[22/Jan/2008:19:37:00-20401-59-2] Process - data
[22/Jan/2008:19:37:00-20401-59-2] Process - datavalue - 2345
[22/Mar/2008:19:37:00-20401-63-2] Process - data
[01/Jul/2008:19:37:00-20401-63-2] Process - data
[22/Jul/2008:19:37:00-20401-63-2] Process - data
[22/Jan/2008:19:37:00-20401-59-2] Process - data11

so here ,first i will grep for 'data11' and i will get the line number as 6.Then from the line6 ,i have to trace back and find out the immediate line which has the same timestamp.here it is [22/Jan/2008:19:37:00-20401-59-2].so the result line is 2.How to get this using grep?
  #2 (permalink)  
Old 07-31-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,089
1. find your desired timestamp
2. find all the lines contain that timestamp
3. get the second last one


Code:
temp=`cat file | grep data11 | nawk '{sub(/\[/,"",$1);sub(/\]/,"",$1);print $1}'`
cat a | grep $temp > file.t
tail -2 file.t | head -1
  #3 (permalink)  
Old 07-31-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
You can avoid the use of temporary files, Useless Use of Cat, and Useless Use of Grep | Awk:

Code:
grep `nawk '/data11/{sub ...}' file` a | tail -2 | head -1
For extra points, modify the awk script so it prints out a sed script which does the right thing.
  #4 (permalink)  
Old 07-31-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
cleaner to do this:

Code:
#  awk '/data11/{print c[$1]};{c[$1]=$0}' file1
[22/Jan/2008:19:37:00-20401-59-2] Process - datavalue - 2345
otherwise you're making assumptions that there are only two lines with that timestamp....
  #5 (permalink)  
Old 07-31-2008
Sharmila_P Sharmila_P is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 19
I am doing some grep operation and some other operations and finding this lineno.From this line I want to go back and find the immediate matched line with that timestamp.
  #6 (permalink)  
Old 07-31-2008
Sharmila_P Sharmila_P is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 19
My code is like this.

for i in $(grep -n data11 file.txt|cut -d: -f1)
do
sed -n "${i},${i}p" logfile.txt|cut -d" " -f5 >> msg.txt
sed -n "${i},${i}p" logfile.txt|cut -d: -f1 >> msg.txt
sed -n "${i},${i}p" logfile.txt|cut -d: -f2,3 >> msg.txt
done


In this after do,i need to get that matched lineno.
  #7 (permalink)  
Old 08-01-2008
Sharmila_P Sharmila_P is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 19
Can anyone tell me How I can get the result?
Closed Thread

Bookmarks

Tags
awk, find previous matching record, nawk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0