Find the answer to your Linux question:
Results 1 to 3 of 3
I have several text files with over 100 lines each. I would like to do a search that will look for the first instance of the keyword "first {", then ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2009
    Posts
    2

    sed/grep question (multiple searches, 1 files)


    I have several text files with over 100 lines each. I would like to do a search that will look for the first instance of the keyword "first {", then search within 10 lines after that keyword for a second keyword "second", and return the file names of the files that match this criteria. In other words, the search looks for the line number that contains "first {", then from that line number, it will search the next 10 lines for "second", and prints out the file name of matches.

    I am not too familiar with grep or sed so any help will be appreciated.

  2. #2
    Just Joined!
    Join Date
    Oct 2004
    Posts
    62
    Hi phreaqo,
    I had to work hard to find a solution (perhaps Linux gurus will ptropose much better solutions)
    but your problem was very interesting to me...
    Code:
    for i in `ls -1 p*.html`;do  > x;echo $i >>x; cat $i| grep -A10 '<title' >>x; cat x|..
        tr  '\n' '~' >y; cat y|grep '<body'|cut -d'~' -f1  ;done
    Now a bit of explanation for this clumsy solution...
    Code:
    for i in `ls -1 p*.html`     # I used as an example several html files
    do                           #      searching tag title and then body (within 10 lines)
    
        > x                                      # create file 'x' with 0 length
        echo $i >>x                              # write the name of html file in x
        cat $i| grep -A10 '<title' >>x           # list each html file and search for '<:tile' 
                                                 #     (appendig the result in x)
        cat x|tr  '\n' '~' >y
        cat y|grep '<body'|cut -d'~' -f1  
                                                 # list x, in a single string (\n -->tilde)
                                                 #     and then search for the 2nd tag
                                                 # having as output only the html name
                                                 #      containg the 2 tags
    done
    Bye.

  3. #3
    Just Joined!
    Join Date
    Feb 2009
    Posts
    2
    Fiomba,

    Thank you so much for your help and the detailed explanation. As an added bonus, your description will also give me a firmer grasp of scripting.

    Many thanks,
    phreaqo

Posting Permissions

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