How to grep (say)last-3 and next-3 lines of Desired Pattern


 
Thread Tools Search this Thread
Operating Systems Solaris How to grep (say)last-3 and next-3 lines of Desired Pattern
# 1  
Old 11-06-2008
How to grep (say)last-3 and next-3 lines of Desired Pattern

Hi All,

OS-Type=Sun-OS 5.8 Sparc9 Processor

Can I grep the previous 4 lines and next 4 lines of a matched pattern(context grep)?
For example here we need to monitor logs of live traffic.The data obtained from "tail -f LiveTrafficData.log"
looks something like this:-
----------------------------
Sender-ip=10.123.32.34 //Useful Info
Junk Parameter=blah
Blah Parameter=more blah
Packet-state=start //Useful Info
Policy-type=test-value-policy //Useful Info
Some more blah
blah blah
user-profile-type=host
user-location=njs-234 //Useful Info
....
...
Similar sort of repetitive data
-------------------------------
Now here the lines which are of concern to me are only policy-type,packet-state,service-ip and user-location,
i.e., I need to ascertain sender-ip and user-location only and only when Policy-type=<desired pattern> AND
Packet-state=<desired pattern2>.

Also note it is from live traffic, so we need to work on "tail -f" of whatever log we are getting,i.e. we need to work on Standard Input and not file.

Any help is welcome.

Thanks and Regards.

P.S:-I went through the internet,but I dont have "grep -A num" or "vmsgrep" or "cgrep".
Also,as it is fresh and accumulating log,so I probably cant store in a file, ie I need to work on Standard Input,not file

Last edited by Sujan Banerjee; 11-06-2008 at 03:25 AM.. Reason: Adding Userful Information
# 2  
Old 11-06-2008
if you dont have grep -A/-B you would do this with awk or perl.
red a line from stdin
store the line in a ringbuffer or in an array.
make shure old lines are deleted from the buffer.
if a line maches a pattern print out the buffer and the line . empty the buffer.
the following 3 lines can be printed by resetting a counter which is encresed every time a line is written.
a nice job for awk and a demanding task to learn it
# 3  
Old 11-06-2008
Hi Demwz
thanx for the reply
But its certainly easier said than done.
Anyway will try to learn Awk/Perl

Thanx
# 4  
Old 11-06-2008
someone else did already
Code:
grep -E -B 19 -A 26 "^pattern +[1-9]" input.file
# or
awk '$1=="pattern" && $2>0{for(i=0;i<19;i++)print a[(i+NR)%19];print;for(i=0;i<26;i++){getline;print}}{a[NR%19]=$0}' input.file

see http://www.tek-tips.com/viewthread.cfm?qid=1214284
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep all lines with the pattern .sh

Linux version : Oracle Linux 6.5 Shell : bash In the the below text file (someString.text), I want to grep all lines with .sh in it. ie. Only the lines mysript.sh and anotherscript.sh should be returned. My below attempts failed. I gather that in regular expression world, dot (.) is the... (3 Replies)
Discussion started by: John K
3 Replies

2. Shell Programming and Scripting

Find pattern; grep n lines before and after

Hi, I need help to grep a specific part of a log file (bold). 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: } 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: extensionContainer <Not Present> 24/2/2017-16:57:17.056... (8 Replies)
Discussion started by: vasil
8 Replies

3. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

4. Shell Programming and Scripting

Grep lines before a pattern having some other pattern

Hi All, I am trying to fetch lines before a pattern, I got to know about -B flag in grep but we have to pass the number to get those lines before some pattern say (X), now what if I want to get line/s with some other pattern say (Y) before X pattern? How to get about it? please help. Input:... (5 Replies)
Discussion started by: dips_ag
5 Replies

5. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. Shell Programming and Scripting

grep: get last 3 lines containing PATTERN from many files

Hi all, I am looking for a quick solution for this: I have many log files of an iterative program, and I would like to display the parameters of the last three iteration from each of those files. Relevant lines have the keyword: ITER I am using: tac ~/modeling*/fitting.log | grep -m 3 -e... (2 Replies)
Discussion started by: pnemeth
2 Replies

7. Shell Programming and Scripting

Print the above and below lines for the grep pattern.

Hi, i would like to get the above and below lines of the grep pattern . For ex : file as below: chk1- aaaa 1-Nov chk2 -aaaa ########## chk1-bbbbbb 1-Nov chk2-bbbbbb ######### my search pattern is date : 1-Nov i need the o/p as below chk1- aaaa 1-Nov (6 Replies)
Discussion started by: expert
6 Replies

8. UNIX for Dummies Questions & Answers

Grep with 8 lines before and after pattern.

OK. I have a file I'd like to be able to grep, but on top of returning the line where the pattern matches, I'd like to be able to get the previous 8 lines and the following 8 lines. Is there a way to do this? (2 Replies)
Discussion started by: mrwatkin
2 Replies

9. UNIX for Dummies Questions & Answers

grep required pattern and next 2 or 3 lines

dear ones pl.kindly help me 1) how to print(grep) required pattern and following 2 or 3 lines. 2) grep required pattern(to print)+above 2 lines+below 2 or 3 lines.from a report file. ex: we have some report file kf askfjsk fksaj fk skf sjfksjd kff sjfkjs kf jskdjfklsd jfklsdf sdkfjsd fsd... (3 Replies)
Discussion started by: cvvsnm
3 Replies

10. Shell Programming and Scripting

grep to show lines only after pattern

When i grep for a pattern the search results comes up with matching lines(some before the pattern and some after)...how can i limit the search so that it shows only the lines after the pattern specified (5 Replies)
Discussion started by: wannalearn
5 Replies
Login or Register to Ask a Question