awk logic for tailing and printing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk logic for tailing and printing
# 8  
Old 11-24-2013
Quote:
Originally Posted by Scrutinizer
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 worked!!!

anyway I can add a "else" into it to print the entire output in the event the last line of the output doesn't contain the "FAILED" string?

so the logic i'm after is,

if the last line of the output contains a string, print x amount of lines in the output received. this requirement has been satisfied with your awk code.

the next logic is, if the last line of the log does not contain the string "FAILED", then print the entire output. no need to print a select number of lines. just print everything
# 9  
Old 11-24-2013
Try this might help

Code:
$ process Blah Blah | awk '{print >"tmp"}END{print /Failed/ ? NR : 99 }' | \
while read line; do [ "$line" -ne 99 ] && awk -v start=$line 'NR>=(start-200) && NR<start' tmp && rm -rf tmp || cat tmp && rm -rf tmp;done

OR

Code:
$ process Blah Blah | awk '{print >"tmp"}END{print /Failed/ ? NR : 99 }' | \
while read line; do [ "$line" -ne 99 ] && tail -200 tmp && rm -rf tmp || cat tmp && rm -rf tmp;done


Last edited by Akshay Hegde; 11-24-2013 at 12:52 PM..
This User Gave Thanks to Akshay Hegde For This Post:
# 10  
Old 11-24-2013
You could try:
Code:
awk '{n=$0 ORS; p=p n; f=f n} NR>m{ sub(/[^\n]*\n/,x,p)} END{printf "%s", n~s?p:f}' m=200 s=FAILED

But if the output is really lengthy it might run out of memory.

------edit------
A bit more efficient:
Code:
awk '{A[NR]=$0} END{b=A[NR]~s?NR-m+1:1; for(i=(b>0)?b:1; i<=NR; i++) print A[i]}' m=200 s=FAILED


Last edited by Scrutinizer; 11-24-2013 at 01:01 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 11-24-2013
@ Scrutinizer
whether this ?

Code:
$ awk '{print >"tmp"}END{ system(sprintf("%s", /FAILED/? "tail -200 tmp" : "cat tmp" ))  }'

to delete tmp
Code:
$ awk '{print >"tmp"}END{ system(sprintf("%s", /FAILED/? "tail -200 tmp && rm -rf tmp" : "cat tmp && rm -rf tmp" ))  }'

# 12  
Old 11-24-2013
@Akshay, yes something like that should work. I would avoid using rm -rf like that (risky), simply rm. Also you need to know if the temporary file exists and if not use something else, or use a temporary file name that is guaranteed not to exist, and typically use something in the /var/tmp directory..
# 13  
Old 11-24-2013
Quote:
Originally Posted by Scrutinizer
You could try:
Code:
awk '{n=$0 ORS; p=p n; f=f n} NR>m{ sub(/[^\n]*\n/,x,p)} END{printf "%s", n~s?p:f}' m=200 s=FAILED

But if the output is really lengthy it might run out of memory.

------edit------
A bit more efficient:
Code:
awk '{A[NR]=$0} END{b=A[NR]~s?NR-m+1:1; for(i=(b>0)?b:1; i<=NR; i++) print A[i]}' m=200 s=FAILED

this is just perfect!

i'm curious, how did you become so proficient at awk? did you take a specific class? i'm pretty sure they dont go into any significant lenght of training on it at any colleges. so i wanna know how you became so good at it that you can pretty much come up with solutions to any problem (using awk). is awk your favorite programming language? if not, what is? i know this has nothing to do with this thread, i'm just curious.
# 14  
Old 11-24-2013
@Scrutinizer
Yes as you said rm -rf is risky and even its better to use something else instead oftmp. I think from this (post #11 system call) we can overcome from out of memory problem, if file is very lengthy right ?
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