Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2005
    Posts
    16

    Unanswered: 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

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    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'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •