![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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? |
|
||||
|
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
|
|
||||
|
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
|
|
||||
|
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. |
![]() |
| Bookmarks |
| Tags |
| awk, find previous matching record, nawk |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|