Find the answer to your Linux question:
Results 1 to 6 of 6
Does anyone know how to use grep to list not only the lines where there is a match, but also the lines after the match as well? For example. in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    10

    How to use grep to list lines below the lines with a match?


    Does anyone know how to use grep to list not only the lines where there is a match, but also the lines after the match as well? For example. in the blew log file, i want to be able to print all lines after Wednsday Oct 19.


    Wed Oct 19 21:11:39 2011
    FBDA started with pid=30, OS id=22828
    replication_dependency_tracking turned off (no async multimaster replication found)
    Errors in file /u01/app/oracle/diag/rdbms/loc/loc/trace/loc_ora_21840.trc:
    ORA-00942: table or view does not exist
    Starting background process QMNC
    Wed Oct 19 21:11:39 2011
    QMNC started with pid=32, OS id=22832
    Error 604 in kwqmnpartition(), aborting txn

  2. #2
    Linux User Krendoshazin's Avatar
    Join Date
    Feb 2005
    Location
    London, England
    Posts
    471
    Use the -B and -A options in grep for 'before' and 'after' respectively:
    Code:
    > grep -A5 main olddir/apr-util-1.4.1/LICENSE
          of this License, Derivative Works shall not include works that remain
          separable from, or merely link (or bind by name) to the interfaces of,
          the Work and Derivative Works thereof.
    
          "Contribution" shall mean any work of authorship, including
          the original version of the Work and any modifications or additions

  3. #3
    Just Joined!
    Join Date
    Aug 2012
    Posts
    10
    Quote Originally Posted by Krendoshazin View Post
    Use the -B and -A options in grep for 'before' and 'after' respectively:
    Code:
    > grep -A5 main olddir/apr-util-1.4.1/LICENSE
          of this License, Derivative Works shall not include works that remain
          separable from, or merely link (or bind by name) to the interfaces of,
          the Work and Derivative Works thereof.
    
          "Contribution" shall mean any work of authorship, including
          the original version of the Work and any modifications or additions
    Thanks you

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Aug 2012
    Posts
    10
    Is there an end of file option for grep -A or is it possible to make the number of lines dynamic? For instance, as soon as I find a match, how do I use grep to print all lines until the end of file without knowing the exact number of lines.

  6. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.
    Code:
    sed -n '/Wed Oct 19/,$p' <input-filename>
    See man sed for details.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  7. #6
    Just Joined!
    Join Date
    Aug 2012
    Posts
    10
    Quote Originally Posted by drl View Post
    Hi.
    Code:
    sed -n '/Wed Oct 19/,$p' <input-filename>
    See man sed for details.

    Best wishes ... cheers, drl
    Thanks for your help mate.

Posting Permissions

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