0

I want to grep for some specific lines from a log file, and then capture a specific part of that output in a variable and use it in other commands.

The grep command I have is the following, where $1 is a date:

grep -a --binary-file=text "pattern" /home/path/archive/logs/path.log-$1

Example output:

/home/path/archive/logs/path.log-2015-04-13.0.gz:2015-05-13 00:43:49,779 INFO [DEUX-DR-SAMPLE-1] c.i.s.p.DeuxProxyPMMProcessor [DEUX : 361] SVRREQ|dataID|server request: (deliver: (pdu: 0 5 0 282190) (addr: 1 1 adress)  (addr: 1 1 mssidn)  (sm: enc: ASCII msg: id:dataID stat:pattern)

From this output I want to take only the dataID field and save it in a variable for use in other commands. There are hundreds of lines like this though, and the number of spaces can't be used as a delimiter to get the dataID field because it varies from line to line.

1 Answer 1

1

Use grep with PCRE:

$ var='/home/path/archive/logs/path.log-2015-04-13.0.gz:2015-05-13 00:43:49,779 INFO [DEUX-DR-SAMPLE-1] c.i.s.p.DeuxProxyPMMProcessor [DEUX : 361] SVRREQ|dataID|server request: (deliver: (pdu: 0 5 0 282190) (addr: 1 1 adress)  (addr: 1 1 mssidn)  (sm: enc: ASCII msg: id:dataID stat:pattern)'

$ grep -Po '.*?SVRREQ\|\K[^|]+(?=\|)' <<<"$var"
dataID

To save it in a variable:

$ foobar="$(grep -Po '.*?SVRREQ\|\K[^|]+(?=\|)' <<<"$var")"
16
  • what is this line for grep -Po '.*?SVRREQ\|\K[^|]+(?=\|)' <<<"$var"
    – Asif
    May 14, 2015 at 10:57
  • @Asif I have saved the line you have provided as a variable named var, i thought it would be easier to understand..in case of a file if you use e.g. for line in file or while read line, then you can use foobar="$(grep -Po '.*?SVRREQ\|\K[^|]+(?=\|)' <<<"$line")"
    – heemayl
    May 14, 2015 at 11:03
  • @Asif If you are not clear enough please let me know (and with a precise question)..
    – heemayl
    May 14, 2015 at 11:22
  • ok, so if i want to run another command in the same script file eg: grep $var /path/to/another/file.log.. does it return an output line-by-line based on output in variable ( var )?
    – Asif
    May 14, 2015 at 12:29
  • @Asif: If you have multiple files like the one you provided in a file e.f. file.txt, then running grep -Po '.*?SVRREQ\|\K[^|]+(?=\|)' file.txt will give you all dataID found in the file..you should not use the variable in this case, you can use an array instead to save all the outputs e.g. foobar=( "$(grep -Po '.*?SVRREQ\|\K[^|]+(?=\|)' file.txt )......on the other hand if you are reading file line by line then use foobar="$(grep -Po '.*?SVRREQ\|\K[^|]+(?=\|)' <<<"$line")", where $line` represents each line..
    – heemayl
    May 14, 2015 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .