3

I'd need to extract the value of a variable "error" from a log file. Here's a sample line:

WARN (Periodic Recovery) IJ000906: error=15 check server.log

I'd need to capture the value of "error". Looking on similar answers, I've come up with:

echo "WARN (Periodic Recovery) IJ000906: error=15 check server.log" |  grep -P '\d+ (error=?)' -o

However it does not produce any value. Can you recommend a working solution for this case?

3
  • 2
    ? is an operator, not a wild-card. You are trying to match the string error followed by an optional =. Commented Nov 29, 2021 at 18:44
  • and you're trying to match digits before a blank before error when the character that precedes the blank before error is :, not a digit. Commented Nov 29, 2021 at 18:57
  • Why do you have to use POSIX shell? With bash or zsh, you would have much more possibilities. Commented Nov 30, 2021 at 10:27

6 Answers 6

4

Using sed

$ echo "WARN (Periodic Recovery) IJ000906: error=15 check server.log" | sed 's/.*error=\([^ ]*\).*/\1/'
15
Sign up to request clarification or add additional context in comments.

Comments

4

For a perl-compatible regular expression, you're looking for a "lookbehind" assertion.

To find digits that are preceded by the string "error=", you want:

echo "$line" | grep -o -P '(?<=error=)\d+'    # => 15

See the pcresyntax(3) man page

Comments

2

I would use GNU AWK following way, let file.txt content be

WARN (Periodic Recovery) IJ000906: error=15 check server.log

then

awk 'BEGIN{FPAT="error=[0-9]+"}{print substr($1,7)}' file.txt

output

15

Explanation: I inform GNU AWK that column is error= followed by 1 or more digits using field pattern (FPAT), for every line print first field starting from 7th charater, using substr string function. 7 as error= has 6 characters. Note: this solution will print first occurence of error=value for each line.

(tested in gawk 4.2.1)

Comments

2

You may use this grep:

s='WARN (Periodic Recovery) IJ000906: error=15 check server.log'
grep -oP '\berror=\K\d+' <<< "$s"

15

RegEx Details:

  • \b: Match word boundary
  • error=: Match error= text
  • \K: Reset matched info
  • \d+: Match 1+ digits and print it

2 Comments

I'd suggest replacing \d+: with \b -- I don't think that the previous digits-colon-space are part of the requirements.
Thanks @glennjackman! Reason why I used that because OP was using \d+ (error=?) as regex pattern i.e. trying to match 1+digits before whitespace + error.
1

With bash >= 3.0.

v='WARN (Periodic Recovery) IJ000906: error=15 check server.log'

[[ $v =~ error=([0-9]+) ]] && echo "${BASH_REMATCH[1]}"

Output:

15

Comments

1

1st solution: With your shown samples, please try following awk code.

awk -F'error=| check' '{print $2}' Input_file

Explanation: Simple explanation would be, setting field separators as error= OR check for all the lines. Then printing 2nd field of line, which will print value after error= and before check as per shown samples.



2nd solution: Using match function of awk here.

awk 'match($0,/error=[^[:space:]]+/){print substr($0,RSTART+6,RLENGTH-6)}' Input_file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.