How to grep a word and the two lines before?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to grep a word and the two lines before?
# 1  
Old 11-09-2011
How to grep a word and the lines before?

Hi

I have this txt file getting from a lpstat command.
Code:
XA40 XA40 0 Unknown 0 0 1 1 1
0 Unknown LPD 0 0 1 1 2
0 Unknown specified 0 0 1 1 3
XA99 @spip READY 
: (FATAL ERROR) 0781-233 Unknown host spiprs01.mon.local.
XA01 @xs00 READY 
XA01 XA01 0 Unknown 0 0 1 1 1
0 Unknown LPD 0 0 1 1 2
0 Unknown specified 0 0 1 1 3
XA07 @xs00 READY 
XA07 XA07 0 Unknown 0 0 1 1 1
0 Unknown LPD 0 0 1 1 2
0 Unknown specified 0 0 1 1 3
0 Unknown specified 0 0 1 1 3
0 Unknown 0 0 1 1 4
0 Unknown Status 0 0 1 1 5
0 Unknown 0 0 1 1 6
xA45 @diwp READY 
: (FATAL ERROR) 0781-233 Unknown host diwpsnt1.dgw.unix.com
XA32 @xs00 READY 
XA32 XA32 0 Unknown 0 0 1 1 1
0 Unknown LPD 0 0 1 1 2
0 Unknown 0 0 1 1 3
0 Unknown 0 0 1 1 4
0 Unknown Status 0 0 1 1 5
0 Unknown 0 0 1 1 6
XA64 @xs00 READY 
XA64 XA64 0 Unknown 0 0 1 1 1
0 Unknown LPD 0 0 1 1 2
 
XA10 @xs00 READY 
XA10 XA10 0 Unknown 0 0 1 1 1
0 Unknown LPD 0 0 1 1 2
0 Unknown specified 0 0 1 1 3
XA48 @xs00 READY 
XA48 XA48 0 Unknown 0 0 1 1 1
0 Unknown LPD 0 0 1 1 2
0 Unknown specified 0 0 1 1 3
XA68 @xs00 READY 
XA68 XA68 0 Unknown 0 0 1 1 1
0 Unknown LPD 0 0 1 1 2
0 Unknown specified 0 0 1 1 3
0 Unknown 0 0 1 1 4

How can i grep the word "ERROR" and the lines before (where are the id of the printer in error) and send the result to my mail?

Last edited by npatao71; 11-10-2011 at 06:30 AM.. Reason: Please use code tags
# 2  
Old 11-09-2011
If you are on Linux or have gnu tools (specific gnu grep)

Code:
grep -B 2  'FATAL ERROR'  inputfile

If not let us know.
# 3  
Old 11-10-2011
Hi

I have a AIX Version 5.3!
I try that, but for some reason, the first line with the printer id doesn't shows up!!!
# 4  
Old 11-10-2011
what is the expected output for the above posted sample ?
# 5  
Old 11-10-2011
Something like that:
Code:
XA99 @spip READY 
: (FATAL ERROR) 0781-233 Unknown host spiprs01.mon.local.
xA45 @diwp READY 
: (FATAL ERROR) 0781-233 Unknown host diwpsnt1.dgw.unix.com

Moderator's Comments:
Mod Comment You were already asked to use code tags above - please do so. You got a PM with instructions.

Last edited by zaxxon; 11-10-2011 at 06:08 AM.. Reason: code tags, see PM
# 6  
Old 11-10-2011
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print few lines before and after matching word is found suing grep?

Hi, here are few lines present in the logs. I want to grep on Error and print few lines before and after Error word is found line1 Line2 Line3 Error Line4 Line5 Line6 Line7 I want the output to be Line2 Line3 Error Line5 (1 Reply)
Discussion started by: arghadeep adity
1 Replies

2. Shell Programming and Scripting

Grep word after last occurance of string and display next few lines

Hi, I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below... (17 Replies)
Discussion started by: nes
17 Replies

3. Shell Programming and Scripting

Need a word which just comes next to after grep of a specific word

Hi, Below is an example : ST1 PREF: int1 AVAIL: int2 ST2 PREF :int1 AVAIL: int2 I need int1 to come in preferred variable while programming and int2 in available variable Please help me doing so Best regards, Vishal (10 Replies)
Discussion started by: Vishal_dba
10 Replies

4. 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

5. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

6. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

7. Shell Programming and Scripting

grep lines containing a word only twice.

Hi, I have a query on using grep options. I tried with several options but unable to do it. lanite:52> cat note 123 456 ab 123 ab cv 234 4566 67 ab gh tij ab 12 34 ab ab cv dfgv ab cv ab kjhk ab ghj sdf dfg ab jljklj ab Now, I need to use grep to find the line which contains the... (6 Replies)
Discussion started by: dilipr25
6 Replies

8. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

9. Shell Programming and Scripting

GREP:Output all lines containing word

Output all lines in the file temp that contain the word dog using GREP only and in one line!!! I tried grep ']dog]' temp but it doesnt catch word dog when is at beginning or end, like: Our dog is nice /this OK Nice dog /this NOT dog good /this NOT Thank... (3 Replies)
Discussion started by: ljubayuu
3 Replies
Login or Register to Ask a Question