If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > print previous line

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-24-06, 13:56
hoffa4444 hoffa4444 is offline
Registered User
 
Join Date: Jul 2005
Posts: 16
print previous line

Hello,

I have numerous files with tons of information in them. Everything stays the same (format wise) each time the file is created. What I want to do is search for a specific word(s) and print the line directly above it into another file. The word(s) I'll be searching for will remain the same every time. The file will look something like this:

my name is earl (guy)
say: hello
good: bye
etc
no

my name is frank (crazy)
say: goodbye
hello:
etc
yes

What I want to do is print the first line above every section that has "say: hello" on one line into another file. And get rid of the (* after the line im printing. I don't have any issues finding the "say: hello". I'm having trouble printing the line above "say: hello". Anyone have any ideas as to how I can about printing the line above the keywords i'm looking for?

Thanks in advance,

-Hoffa
Reply With Quote
  #2 (permalink)  
Old 01-24-06, 20:34
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,454
That would be sed.

For example, http://www.student.northpark.edu/pem...d/sed1line.txt

In particular:
Code:
 # print the line immediately before a regexp, but not the line
 # containing the regexp
 sed -n '/regexp/{g;1!p;};h'
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On