Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2003
    Location
    India
    Posts
    114

    Lightbulb Unanswered: searching the line + next 20 lines

    Hello Friends

    I want to search a pattern in a simple file in UNIX. I can use the grep command for this but I want to get the search-pattern line and the next 20 lines redirected in some other file. Is there any option in grep or some other way through which I can get the output.

  2. #2
    Join Date
    Jan 2006
    Posts
    32
    You can do this with sed or awk. I have always preferred awk to sed, but I think it might be easier to do this one in sed.

    Code:
       sed -n -e '/pattern/,+20p' file
    should do what you want.



    .

  3. #3
    Join Date
    Nov 2003
    Location
    India
    Posts
    114

    Unhappy Not working

    :-)

    bsfbl85:/oq/logs/batch/dl_hist>sed -n -e '/BA7400A/ ,+20p' uoq1002.scriptlog.20060129
    Unrecognized command: /BA7400A/ ,+20p
    bsfbl85:/oq/logs/batch/dl_hist>sed -n -e '/BA7400A/,+20p' uoq1002.scriptlog.20060129
    sed: command garbled: /BA7400A/,+20p


    Sorry the command is not working. Am i doing something wrong or there is some alternate way to achieve this.

  4. #4
    Join Date
    Jan 2006
    Posts
    32
    You must be using a different version of sed than I am. The documentation says that the +n syntax for the second address is a GNU extension. I ran a test of the above command with sed on Fedora Core 3 and it works fine.

    I guess the question is what version of UNIX are you running and can you install GNU's version of sed on it. Otherwise I guess you'll need to use awk to do this.



    .

  5. #5
    Join Date
    Nov 2003
    Location
    India
    Posts
    114

    Exclamation urgent

    Quote Originally Posted by hyperbole
    You must be using a different version of sed than I am. The documentation says that the +n syntax for the second address is a GNU extension. I ran a test of the above command with sed on Fedora Core 3 and it works fine.

    I guess the question is what version of UNIX are you running and can you install GNU's version of sed on it. Otherwise I guess you'll need to use awk to do this.



    .

    Can anybody help with awk or some shell script to achieve the task. Friends this is urgent

  6. #6
    Join Date
    Jan 2006
    Posts
    32
    Place the following code into a file called script.awk:
    Code:
    BEGIN         { count = 21 }
    /pattern/     { count = 0; }
    count <= 20 { print; count++; }
    from the command line run:
    awk -f script.awk file-you-want-to-search



    .

  7. #7
    Join Date
    Nov 2003
    Location
    India
    Posts
    114

    Thanks

    Thanks a lot hyperbole , this worked.

Posting Permissions

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