0

I would like to find line which starts from word: "ERRORS" and exctract number from that line.

Part of file:

...
[ERROR] No keywords and test cases defined in file
File path: libraries_instances.robot

TEST SUITES SUMMARY:
ERRORS:        148
WARNINGS:      89
CS VIOLATIONS: 201

My solution is:

grep ERRORS .validation.log | grep -o -E '[0-9]+'

is it possible to make it better and use only one grep?

Finally I would like to assign that value to variable in my bash script.

pb.
  • 321
  • 3
  • 4
  • 21

1 Answers1

2

Since linux tag is present in question, assuming GNU grep with -P option is available

$ grep -oP 'ERRORS.*\h\K\d+' .validation.log
148
  • ERRORS.*\h\K here the \K option helps to mark the starting point of regex.. string matched up to this point won't be part of output
  • also note that man grep warns about using -P as experimental, but I haven't faced any issue so far.. (see https://debbugs.gnu.org/cgi/pkgreport.cgi?package=grep for known GNU grep issues)


Alternate solution using awk

$ awk '/ERRORS:/ && NF==2{print $NF}' .validation.log
148
  • /ERRORS:/ && NF==2 match line containing ERRORS: and has only two fields (by default, one or more contiguous whitespace is field delimiter)
  • print $NF print the last field
Sundeep
  • 23,246
  • 2
  • 28
  • 103