Home » Infrastructure » Unix » Grep to get previous line (HP Unix)
Grep to get previous line [message #419154] Thu, 20 August 2009 02:19 Go to next message
gkrishn
Messages: 487
Registered: December 2005
Location: Putty a dark screen
Senior Member
grep "END OF STMT" oemdb_ora_17616_MYSQLTRACE.trc

END OF STMT
END OF STMT
END OF STMT
END OF STMT
END OF STMT
END OF STMT


any ways to grep to get the line just above the "END OF STMT" line. is there a way to do it. Thanks
Re: Grep to get previous line [message #419161 is a reply to message #419154] Thu, 20 August 2009 02:54 Go to previous messageGo to next message
Michel Cadot
Messages: 57613
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Use awk or perl or even sed.

Regards
Michel
Re: Grep to get previous line [message #419165 is a reply to message #419154] Thu, 20 August 2009 03:57 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10613
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
Depends on the complexity of search.
A simple one-liner with awk is a an old standard.
Any more complex, the real solution is with perl.
Kaapi:ora magvivek$ cat somelog.log
first line
second line
third line
fourth line
fifth line
sixth line above duplicate second line
duplicate second line

Kaapi:ora magvivek$ 

##author: not me. Shamelessly ripped from an old logRotate.
##b=number of lines before
##a=number of lines after
##s=value

Kaapi:ora magvivek$ awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=1 a=0 s="second" somelog.log
first line
second line
fifth line
sixth line above duplicate second line
sixth line above duplicate second line
duplicate second line
Re: Grep to get previous line [message #419172 is a reply to message #419165] Thu, 20 August 2009 04:36 Go to previous messageGo to next message
tahpush
Messages: 961
Registered: August 2006
Location: Stockholm/Sweden
Senior Member

If you just want the line above
A bit ugly, but it servs the purpose

grep -B 1 "END OF STMT" myfile | grep -v "END OF STMT"

[Updated on: Thu, 20 August 2009 04:38]

Report message to a moderator

Re: Grep to get previous line [message #419285 is a reply to message #419154] Thu, 20 August 2009 23:05 Go to previous messageGo to next message
gkrishn
Messages: 487
Registered: December 2005
Location: Putty a dark screen
Senior Member
Thanks Both. it was realy helpful info.
icon14.gif  Re: Grep to get previous line [message #426598 is a reply to message #419172] Fri, 16 October 2009 16:03 Go to previous message
scripter
Messages: 6
Registered: October 2009
Location: India
Junior Member
Nice usage of grep command Smile
Previous Topic: calling procedure in shell script (merged)
Next Topic: Migrating 10.7 /8i Apps to Solaris 10/Solaris 8 container
Goto Forum:
  


Current Time: Sun Apr 20 14:31:30 CDT 2014

Total time taken to generate the page: 0.08019 seconds