Search code examples
linuxbashgrepsolaris

Grep: using with -e and -o options together


Need to parse big log file in one run and print id, address and service_name of found requests. The problem is that service_name is in request body that is quite big.

If I list all patterns with -e option -

grep -e 'ID: [0-9]\+' -e 'Address: .*' -e ':Body><[^ ]*'

the full request body will be printed.

What is needed is

grep -e 'ID: [0-9]\+' -e 'Address: .*' -o ':Body><[^ ]*'

or

grep -o 'ID: [0-9]\+' -o 'Address: .*' -o ':Body><[^ ]*'

to print only first word from request body that is name of the service;

but in this case grep: :Body><[^ ]*: No such file or directory error received

UPD: solution with -oe and regex works, but as it turned out -o significantly slows the operation


Solution

  • If you wish to print only bits of a file that match these 3 regular expressions and those three are never on the same line, you may use \|, which is grep's logical or :

    grep -o 'ID: [0-9]\+\|Address: .*\|:Body><[^ ]*' my.log