Computing.Net > Forums > Unix > String search

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.

String search

Reply to Message Icon

Name: jbritto
Date: December 15, 2004 at 10:54:09 Pacific
OS: Solaris
CPU/Ram: N/A
Comment:

Hi

I need to display a line and its previous line that has search string from bottom of the file.
For ex. My search string is “One” and the data file is as below

First Occurrence
One
Two
Second Occurrence
One
Two
Third Occurrence
One
Two
Expecting output as :

Third Occurrence
One

Thank you for your help.

Jb



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: December 15, 2004 at 12:50:19 Pacific
Reply:

awk '\
{ if (match($0,"One"))
    {hold_prevline=prevline
     hold_currline=$0}
  prevline=$0
}

END \
{ if (hold_currline=="")
     print "No match found."
  else
    {print hold_prevline
     print hold_currline}
}' datafile


0
Reply to Message Icon

Related Posts

See More


script on crontab Installing Sendmail on SC...



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: String search

shell script for string search www.computing.net/answers/unix/shell-script-for-string-search/5478.html

Search string and how to get it replaced www.computing.net/answers/unix/search-string-and-how-to-get-it-replaced/2517.html

Search file & add string after that www.computing.net/answers/unix/search-file-amp-add-string-after-that/6343.html