Computing.Net > Forums > Programming > Print PreviousLine Using AWK?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Print PreviousLine Using AWK?

Reply to Message Icon

Name: vinodjadye
Date: July 11, 2007 at 12:40:46 Pacific
OS: Linux
CPU/Ram: Intel
Product: Intel
Comment:

Hi,
I need to search the string in a file & then print the 12th line before the line where this string occurs (so basically print 12th line previous to this line). I need to know how can i do this using awk?
if there is any other way anyone can suggest i would be glad.


Vinod




Response Number 1
Name: Mechanix2Go
Date: July 12, 2007 at 02:11:03 Pacific
+1
Reply:

I've been told there's a 'unix provider' for NT. [cyjwin?] And I guess I should get it and learm something.

Is the an 'NT provider' for unix?

anyone?


=====================================
If at first you don't succeed, you're about average.

M2




Response Number 2
Name: ghostdog
Date: July 12, 2007 at 04:19:04 Pacific
+1
Reply:

here's one way
[code]
awk 'NR==FNR{ array[++c]=$0;next }
/search_string/ {print array[FNR-12]}
' "file" "file"
[/code]



Response Number 3
Name: Razor2.3
Date: July 12, 2007 at 21:12:54 Pacific
+1
Reply:

Mechanix2Go Is the an 'NT provider' for unix?

There's Wine



Response Number 4
Name: Mechanix2Go
Date: July 13, 2007 at 00:26:23 Pacific
+1
Reply:

Hi Razor2.3

I forgot about WINE. I tried it way back when, maybe Mandrake 5x. It was shakey. Hopefully ut has improved in the interim.


=====================================
If at first you don't succeed, you're about average.

M2




Response Number 5
Name: vinodjadye
Date: July 19, 2007 at 16:50:24 Pacific
+1
Reply:

Hi,
I am pasting the contents of the file below. I will get similar occurances more than 50 times in one file. I need to search for the string "End the test" and then print 11th or 12th line before this line (the one with timestamps). Please test your code & send it to me.
Thx in advance


Timestamp: Wed Jul 18 04:04:23 PM EDT 2007

Starting Traffic for 20 seconds...
Frame Size: 100
2/3 -> TX Frames: 1132112
2/3 -> RX Frames: 1132112
2/3 -> RX Min Latency: 314.02 usec
2/3 -> RX Max Latency: 44480.08 usec
2/3 -> RX AVG Latency: 6103.62 usec
2/3 -> 2/3 Loss: 0
2/3 -> 2/3 Loss Ratio %: 0.000000000

End the test, we are at max rate: 5.133056640625

Frame Sizes (bytes): 100
Rate (%): 5.13
Avg Latency (usec): 6103.62
Min Latency (usec): 314.02
Max Latency (usec): 44480.08


Vinod



Related Posts

See More



Reply to Message Icon

Comparison of strings in ... Batch file to find drive ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Google Ads



Results for: Print PreviousLine Using AWK?

How to compare 2 files using awk? www.computing.net/answers/programming/how-to-compare-2-files-using-awk/15598.html

Using awk in shell script www.computing.net/answers/programming/using-awk-in-shell-script/20453.html

Help.... awk www.computing.net/answers/programming/help-awk/1076.html