Find the answer to your Linux question:
Results 1 to 2 of 2
I want to go through a log file and find pattern1 and then a pattern2 only after pattern 1. e.g. I have this in my log file: start time for ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    12

    find a pattern after a specific pattern


    I want to go through a log file and find pattern1 and then a pattern2 only after pattern 1. e.g. I have this in my log file:

    start time for the job: 12:50
    .
    .
    .
    howManyRecords: 2230
    .
    .
    .
    start time for the job: 13:30
    .
    .
    .
    howManyRecords: 1200
    .
    .
    .
    start time for the job: 14:10
    .
    .
    .
    howManyRecords: 1450
    .
    .
    .

    So for example I want to know howManyRecords was in 13:30.

    I figured I grep for "start time for the job" and then only after that (and before the next occurence of that) grep for "howManyRecords". Is this a sane way?

    Any better ideas to how go about this?

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    649
    Quote Originally Posted by realtest View Post
    I want to go through a log file and find pattern1 and then a pattern2 only after pattern 1. e.g. I have this in my log file:

    start time for the job: 12:50
    .
    .
    .
    howManyRecords: 2230
    .
    .
    .
    start time for the job: 13:30
    .
    .
    .
    howManyRecords: 1200
    .
    .
    .
    start time for the job: 14:10
    .
    .
    .
    howManyRecords: 1450
    .
    .
    .

    So for example I want to know howManyRecords was in 13:30.

    I figured I grep for "start time for the job" and then only after that (and before the next occurence of that) grep for "howManyRecords". Is this a sane way?

    Any better ideas to how go about this?
    Doing this:
    Code:
    grep -E '^start time for the job|^howManyRecords' logfilename |  grep -A 1 '13:30' | tail -n 1

Posting Permissions

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