awk logic for tailing and printing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk logic for tailing and printing
# 1  
Old 11-24-2013
awk logic for tailing and printing

if the last line of an output contains a certain string 'FAILED', i want to print 200 lines from the output.

here's where I got stuck:

Code:
process blah blah blah | awk '{if ($0 ~ /FAILED/) y=x "\n" $0; x=$0};END{print y}'

the above only prints the last 2 lines, and it also searches the entire output for the word "FAILED". i dont want to search the entire output as it can be pretty large. i just want to check the last line of the output, and if this last line contains the word "FAILED", i want the awk to then print the last 200 lines of the output.

so basically, printing the last 200 lines of the output should be dependent only on IF the last line of the output contains the word "FAILED".
# 2  
Old 11-24-2013
Quote:
Originally Posted by SkySmart
if the last line of an output contains a certain string 'FAILED', i want to print 200 lines from the output.

here's where I got stuck:

Code:
process blah blah blah | awk '{if ($0 ~ /FAILED/) y=x "\n" $0; x=$0};END{print y}'

the above only prints the last 2 lines, and it also searches the entire output for the word "FAILED". i dont want to search the entire output as it can be pretty large. i just want to check the last line of the output, and if this last line contains the word "FAILED", i want the awk to then print the last 200 lines of the output.

so basically, printing the last 200 lines of the output should be dependent only on IF the last line of the output contains the word "FAILED".
without input its really difficult for us to guess what you are trying to do

You can do something like this

Not Tested just wild guess
Code:
$ process blah blah blah |awk '/Failed/{end=NR;start=NR-200;next}FNR>=start && FNR<=end{print}' file file

# 3  
Old 11-24-2013
Quote:
Originally Posted by Akshay Hegde
without input its really difficult for us to guess what you are trying to do

You can do something like this

Not Tested just wild guess
Code:
$ process blah blah blah |awk '/Failed/{end=NR;start=NR-200;next}FNR>=start && FNR<=end{print}' file file

suppose i'm tailing the /var/log/syslog file, i tried the above and it doesn't appear to work.
Code:
tail -500 /var/log/syslog | awk '/FAILED/{end=NR;start=NR-200;next}FNR>=start && FNR<=end{print}'

i left out the the "file file" in your example because that wont be necessary. what I want to do is output the 200 lines if and only if the last line of the file contains an entry and the entry contain "FAILED". some times the last line of a file or output can be blank. so i also need to account for that.

btw, the "tail -500" in my example is just a good example that explains the type of manner if which the data i'm working on is given to me. so basically, i would have to run the awk command on the output produced by a process.
# 4  
Old 11-24-2013
Quote:
Originally Posted by SkySmart
suppose i'm tailing the /var/log/syslog file, i tried the above and it doesn't appear to work.
Code:
tail -500 | awk '/FAILED/{end=NR;start=NR-200;next}FNR>=start && FNR<=end{print}'

i left out the the "file file" in your example because that wont be necessary. what I want to do is output the 200 lines if and only if the last line of the file contains an entry and the entry contain "FAILED". some times the last line of a file or output can be blank. so i also need to account for that.

btw, the "tail -500" in my example is just a good example that explains the type of manner if which the data i'm working on is given to me. so basically, i would have to run the awk command on the output produced by a process.
Please provide input. you can use attachment option.
# 5  
Old 11-24-2013
Quote:
Originally Posted by Akshay Hegde
Please provide input. you can use attachment option.
the output really doesn't matter as it is in no way the same every time. it varies. that's why i gave the tailing of the syslog as an example. you never know what the format of the output will be. and it really doesn't matter as we're only concerned about whether or not the output contain a string.
# 6  
Old 11-24-2013
Try:
Code:
awk '{n=$0; p=p n ORS} NR>m{ sub(/[^\n]*\n/,x,p)} END{if(n~s) printf "%s",p}' m=200 s=FAILED

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 11-24-2013
Still on guess

Try:

Just Test
Code:
$ echo | awk 'BEGIN{for(i=1;i<=300;i++)print i}END{print "Failed"}' | \
awk '{print >"tmp"}END{if(/Failed/)print NR }' | \
while read line; do [ ! -z "$line" ] && awk -v start=$line 'NR>=(start-200) && NR<start' tmp && rm -rf tmp;done

In your case

Code:
$ Process blah blah | awk '{print >"tmp"}END{if(/Failed/)print NR }' | \
while read line; do [ ! -z "$line" ] && awk -v start=$line 'NR>=(start-200) && NR<start' tmp && rm -rf tmp;done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Better way to do tailing with awk

my current code: varA=$(tail -200 /var/log/data.txt | egrep -c "Capital|capitol") varB=$(tail -200 /var/log/data.txt | egrep -c "State|Country") varC=$(tail -200 /var/log/data.txt | egrep -c "City|Town") I want to do this a different way. something like: AllVars=$(echo $(tail -200... (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Shell Programming and Scripting

awk logic

I am trying to check my logic on a long awk i'm using. I have about 30 checks that I built into an awk and I "believe" I did this right, but I could be wrong. awk -F\| ' $9 !~ /\/*{1,}*/ $9 ~ /\(-{4}, {2,3}/ $9 ~ /\({6}, {2,3}\)/ $9 ~ /\(\+{5}, {2,3}\)/ $9 ~ /\(\+\+{4}, {2,3}\)/ $9 ~... (8 Replies)
Discussion started by: dagamier
8 Replies

3. Shell Programming and Scripting

How to use this logic with awk?

Hi friends, I am having 2 files, I just want to compare 2 files each containing 2 columns 1st column is lat, and 2nd column is long, if anyone can understand below logic please help me in writing script with awk.. here each field of file2 needs to be compared with std_file main counter=0... (1 Reply)
Discussion started by: Akshay Hegde
1 Replies

4. UNIX for Dummies Questions & Answers

awk logic and math help

Hi, My file has 2 fields and millions of lines. variableStep chrom=Uextra span=25 201 0.5952 226 0.330693 251 0.121004 276 0.0736858 301 0.0646982 326 0.0736858 401 0.2952 426 0.230693 451 0.221004 476 0.2736858 Each field either has a... (6 Replies)
Discussion started by: wyarosh
6 Replies

5. UNIX for Dummies Questions & Answers

Need help in logic using awk command

I have task to find out the min,max, average value of each service for example i searched for " StatementService " $awk '/VST.*StatementService:/{print $3,$4,$19,$22,$25}' performance.log > smp.log $cat smp.log amexgtv VST: : StatementService:1860 StatementService:getCardReference:0... (3 Replies)
Discussion started by: senthil.ak
3 Replies

6. Shell Programming and Scripting

need an awk script/logic

In one data file i have values like this a b c 1 2 e f g 2 3 i j k 3 5 I need to sum up the last 2 columns and make a data file...How i can do that. a b c 1 2 e f g 2 3 i j k 3 5... (8 Replies)
Discussion started by: bobprabhu
8 Replies

7. Shell Programming and Scripting

Help with awk logic

I want to print lines that have "IND" or "ind" or nothing in field 2 or 3 file: output needed: Code i wrote: nawk -F"," '{if(tolower($2||$3) ~"ind"||"")print}' file Help is appreciated (3 Replies)
Discussion started by: pinnacle
3 Replies

8. Shell Programming and Scripting

need a logic to start with awk/ sh

Hi Friends, I got stuck where to start with .. I ve a input file like below. where I want to compare write data with my read data .. The problem is that the read data should be compared with the lastest write data on that address. Note- Both write data & read data are in the same... (8 Replies)
Discussion started by: user_prady
8 Replies

9. Shell Programming and Scripting

need a logic for awk programming

Hello Friends, I have a txt file like below //*Init Start Reg(read,12'h42E,16'h0000); Nop(5628.5); //*Init End //*Main Start Reg(read,12'h42E,16'h0000); Nop(5628.5); //*Main End I want to calculate the values between //* Init Start & //* Init End And //*Main Start & //*Main... (5 Replies)
Discussion started by: user_prady
5 Replies

10. Shell Programming and Scripting

cannot get logic for concatenation awk

Hello friends, I have a problem in printing an array.. Example if my array line contains 4 elements like following line=0002 , line=202200, line=200002, line= 300313 Now one = sprintf line line line line will concatenate my whole array to one. But I am not sure about the... (7 Replies)
Discussion started by: user_prady
7 Replies
Login or Register to Ask a Question