Question

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?

Était-ce utile?

La solution 2

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

Autres conseils

Using awk it can be found in single line:

awk -F 'Job (start|end) time=' 'NF>1{print $2}' file
2014-03-13 06:37:49
2014-03-13 06:37:51

To read both values in variables:

IFS=';' && read v1 v2 < <(awk -F 'Job (start|end) time=' 'NF>1{printf "%s;", $2}' file)

This is simple and easy to read:

grep "Job start time" test.txt | cut -d"=" -f2
grep "Job end time" test.txt | cut -d"=" -f2

This searches for the lines containing your specific string, sets the delimeter as = between the two, and takes the field on the right of it.

With sed:

v1=$(sed -rn 's/.*Job start time=(.*)/\1/p' yourfile)
v2=$(sed -rn 's/.*Job end time=(.*)/\1/p' yourfile)
eval `sed -n 's/^ *Job start time=\(.*\)/v1="\1"/p
s/^ *Job end time=\(.*\)/v2="\1"/p' YourFile`

tested on aix/bash (so no GNU sed)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top