Find the answer to your Linux question:
Results 1 to 2 of 2
I want some log from a file using grep excluding two patterns Ex: $ cat db01_application.log 10:07 AM : Memory sets allocated. 10:12 AM : Log Loading Started. 10.26 AM ...
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    1

    Grep between two patterns



    I want some log from a file using grep excluding two patterns
    Ex:
    $ cat db01_application.log

    10:07 AM : Memory sets allocated.
    10:12 AM : Log Loading Started.
    10.26 AM : Table temp1 loaded.
    10.56 AM : Table temp2 loaded.
    11:23 AM : Log Loaded successfully.
    11:25 AM : Link Esablished.

    From this file I want activities which are happened between these two patterns Log Loading Started and Log Loaded successfully.

    I want following output from this file

    10.26 AM : Table temp1 loaded.
    10.56 AM : Table temp2 loaded.

    Could some one please let me know any idea how to retrieve this particular block from this file

    Thanks

  2. #2
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    gawk to the rescue. Here is one way to do it:
    Code:
    [bevo@four ~]$ cat some-logfile 
    
    10:07 AM : Memory sets allocated.
    10:12 AM : Log Loading Started.
    10.26 AM : Table temp1 loaded.
    10.56 AM : Table temp2 loaded.
    11:23 AM : Log Loaded successfully.
    11:25 AM : Link Esablished.
    
    [bevo@four ~]$ gawk '
    { if ($0 ~ /Log Loaded successfully/) exit ; 
      if ( printit == "true" ) print ; 
      if ($0 ~ /Log Loading Started/) printit = "true" }
    ' some-logfile 
    
    10.26 AM : Table temp1 loaded.
    10.56 AM : Table temp2 loaded.
    Great reference here: The GNU Awk User's Guide

Posting Permissions

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