Search code examples
bashsedtext-parsingstring-parsing

Parse a two values from the file


Part of my file looks like so:

STATUS REPORT FOR JOB: Job_logging
Generated: 2014-03-14 07:05:03
   Job start time=2014-03-13 06:37:49
   Job end time=2014-03-13 06:37:51
   Job elapsed time=00:00:02
   Job status=1 (Finished OK)
      Stage: Oracle_Connector_0, 1 rows input
      Stage start time=2014-03-13 06:37:51, end time=2014-03-13 06:37:51, elapsed=00:00:00
         Link: DSLink2, 1 rows
      Stage: Peek_3, 1 rows input
      Stage start time=2014-03-13 06:37:51, end time=2014-03-13 06:37:51, elapsed=00:00:00

     Status code = 0
     Link: DSLink2, 1 rows

I need to extract values that stand for Job start time and Job end time

So i need 2014-03-13 06:37:49 and 2014-03-13 06:37:51 to be saved into two separate variables: v1 and v2.

How do I do that using BASH?

I've already killed about an hour playing with strings concatanation and sed but still got nothing.

Little help, please?


Solution

  • You can use grep for this:

    $ grep -Po '(?<=Job start time=).*' file
    2014-03-13 06:37:49
    
    $ grep -Po '(?<=Job end time=).*' file
    2014-03-13 06:37:51
    

    It used a look-behind that checks what comes after Job start/end time= in the given file.

    And to store into a variable, use

    $ var=$(grep -Po '(?<=Job end time=).*' file)
    $ echo "$var"
    2014-03-13 06:37:51