Awk print all lines on match?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk print all lines on match?
# 1  
Old 04-07-2011
Awk print all lines on match?

Ok so I can use awk to match a pattern and print the whole line with print $0. Is there any way to just tell awk to print every line of output when the pattern matches?

I'm having it wait for the word error and then print that entire line. But what I actually need to see is all the following lines after the line with the error. So there has got to be some easy way to just tell awk to print everything when there is a pattern match?
# 2  
Old 04-07-2011
Print all line containing 'toto' in file called 'tst' :

Code:
# cat tst
zroezp toto rueziar
roez tutu uezir
roezp toto reziar
zroezp titi rueiar
roezp toto ruezar
roezp tata rueziar
zroezp titi rziar
zroep toto rzir
zrezp tata ruziar
zezp toto rziar
roezp toto rueziar

Code:
# awk '/toto/' tst
zroezp toto rueziar
roezp toto reziar
roezp toto ruezar
zroep toto rzir
zezp toto rziar
roezp toto rueziar

# 3  
Old 04-07-2011
Quote:
I'm having it wait for the word error and then print that entire line. But what I actually need to see is all the following lines after the line with the error.
does it mean, you want all lines that after the line which match word "error" the very first time?

like
Code:
1 blah blah blah
2 oops error
3 error again
4 ok hello world
5....
....
100...

what you are looking for are lines ( 3- 100) ?

then try this:
Code:
awk '{ if (!a && $0~/error/) {a=1;next;}} a'

or

Code:
 awk '/error/,0'  yourInputFile | awk 'NR>1'


Last edited by sk1418; 04-07-2011 at 05:40 PM..
# 4  
Old 04-07-2011
I'll have to try this when I get back into work again. But, basically I'm looping a simple command and I only want to see output when it errors. The rest of the time I don't care and don't want to see anything.

Code:
 while true ; do command | awk '/error/{print $0 , system ("date")}'  ; sleep 1 ; done

The commands output is either going to be OK or error followed by lines of information about the error and why it occured. Could be 3 lines could be 10 you never know. But I need to leave this running for hours and hours. I didn't want to see 10,000 lines of this

OK
OK
OK
OK
OK
OK
Error
OK
OK

So I am just doing the simple awk command so that it only prints the line when it actually fails and gives me an error. My only issue at this point is that I get the Error line but none of the follow up lines. So at least I know when it fails but, I have no details on why.
# 5  
Old 04-07-2011
I see what u mean. it would be good if you could give an example with boundaries, how to distinguish ERROR, detailed error messages and normal output.

without knowing the format of your inputFile, no one could help.
# 6  
Old 04-07-2011
There is no input file its a simple command and the results of the command are my input. There is no way to know what the output will be when the error occurs nor how many lines. I can presume it would be no more than say 10 lines at most. That's why I was hoping I could just find some way to say. If you see this word then print all output that can be printed.

---------- Post updated at 09:06 PM ---------- Previous update was at 05:54 PM ----------

Code:
 awk '{ if (!a && $0~/error/) {a=1;next;}} a'

Well I did this and it is partially successful. It does print the lines after the match. However it doesn't print the original line with the error on it. Thats not critical and I could live without it. Would be cleaner if the original error line printed also.

However the pattern match doesn't seem to work as my original command did. It runs subsequent date command every time it runs the command regardless of a match to the pattern.

Code:
 while true ; do command | awk '{ if (!a && $0~/failed/system ("date")) {a=1;next;}} a' ;sleep 2; done

I wind up getting this as my output when the output is OK. Ideally I only want to see any output when it fails. So I gotta figure out how to get the Date command to only execute when the pattern matches.

Thu Apr 7 19:00:24 PDT 2011
Thu Apr 7 19:00:26 PDT 2011
Thu Apr 7 19:00:28 PDT 2011
Thu Apr 7 19:00:32 PDT 2011
Thu Apr 7 19:00:34 PDT 2011
Thu Apr 7 19:00:36 PDT 2011

So I have two 50% answers so far. I just need to combine the two into one perfect command. But, must sleep!!!!
# 7  
Old 04-08-2011
Post some lines of the output with the OK and Error lines and the desired output.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Match the value & print lines from the match

Hello, I have a file contains two columns. I need to print the lines after “xxx” so i'm trying to match "xxx" & cut the lines after that. I'm trying with the grep & cut command, if there any simple way to extract this please help me. Sample file : name id AAA 123 AAB 124 AAC 125... (4 Replies)
Discussion started by: Shenbaga.d
4 Replies

3. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

6. Shell Programming and Scripting

Print all lines before first match

Hi, I have this file. close block3c block3b block3a open close block2b block2a open close block1a open and I need : open block3a block3b block3c close (1 Reply)
Discussion started by: lasserfox
1 Replies

7. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

8. Shell Programming and Scripting

Print lines before and after pattern match

I am using Solaris, I want to print 3 lines before pattern match pattern 5 lines after pattern match Pattern is abcd to be searched in a.txt. Looking for the solution in sed/awk/perl. Thanks .. Input File a.txt: ================= 1 2 3 abcd 4 5 6 7 8 (7 Replies)
Discussion started by: manuswami
7 Replies

9. Shell Programming and Scripting

how to print all lines from a second match

I am trying to parse iostat output for io issues.. I want to print all lines including second occurance of 'extended' till EOF(end of file). Can we do that using awk or sed one liners or do we need a script for it? (1 Reply)
Discussion started by: kchinnam
1 Replies

10. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies
Login or Register to Ask a Question