Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print prev line? 1

Status
Not open for further replies.

bigdingle

Technical User
Dec 15, 2003
2
US
I'm trying to print the previous line, if a line has the word "OFFLINE" in it. Here is an example....

Store 0101
Store 0102
State : OFFLINE
Store 0103
Store 0104
Store 0105
State : OFFLINE


I would like to print out STORE 0102 and STORE 0105 since those are the lines directly above the lines that contain OFFLINE.

Any help would be greatly appreciated! Thanks.
 
nawk -f big.awk myFile.txt

#----------- big.awk
/OFFLINE/ { print prev; next}
{ prev = $0}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Perfect! That works wonderfull. Thank you so much Vlad.
 
I have two questions on this solution. Can anyone help me?

1. I tried it without &quot;next&quot; and it also works. What is the use of it in this solution?

2. Why it can print previous line but not the current line? &quot;$0&quot; is already assigned to &quot;prev&quot;. How it works?

Thanks!

tikual
 
For each parse, ultimately 'prev' contains $0
If OFFLINE found, it prints prev (contains previous $0)
Nowhere does it say print $0 (current line)


Dickie Bird (:)-)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top