Skip to main content
added 447 characters in body
Source Link
netmonk
  • 1.9k
  • 1
  • 15
  • 20

Given the assumption that all your lines are formated the same (or at least all the lines containing MATCH:), it appears that MATCH: is the 5th element of the line, and the value you want is the 6th one.

Therefore in awk you just have to test if the 5th element is equal to MATCH: and print the 6th element of the line if correct.

$ echo "2017-01-19:31:51 [ABCD:] 37723 - MATCH: 10 [text]" |awk -e '{ if ($5 == "MATCH:") print $6 }' 
    10

EDIT: Given the assumption MATCH: can be anywhere in the line:

  $ echo "2017-01-19:31:51 [ABCD:] 37723 - MATCH: 10 [text]" |awk -e '{ for (x=1; x<NF; x++ ) { if ($x == "MATCH:") {x=x+1; printf("%s\n", $x); break}}}' 
10

Might not be very elegant, but you need to iter through all fields of the line and test each field, which is done with a for loop, and an if test. If test field is matching, then print the next field.

I just added a break to directly jump to the next line and and continue the current field iteration.

On a multi line file:

$ cat terst 
2017-01-19:31:51 [ABCD:] 37723 - MATCH: 10 [text]
2017-01-19:31:51 [ABCD:] 37723 - MATCH: 11 [text]
2017-01-19:31:51 [ABCD:] 37723 - [text]
2017-01-19:31:51 37723 - MATCH: 12 [text]
$ awk -e '{ for (x=1; x<NF; x++ ) { if ($x == "MATCH:") {x=x+1; printf("%s\n", $x); break}}}' terst 
10
11
12

Given the assumption that all your lines are formated the same (or at least all the lines containing MATCH:), it appears that MATCH: is the 5th element of the line, and the value you want is the 6th one.

Therefore in awk you just have to test if the 5th element is equal to MATCH: and print the 6th element of the line if correct.

$ echo "2017-01-19:31:51 [ABCD:] 37723 - MATCH: 10 [text]" |awk -e '{ if ($5 == "MATCH:") print $6 }' 
    10

Given the assumption that all your lines are formated the same (or at least all the lines containing MATCH:), it appears that MATCH: is the 5th element of the line, and the value you want is the 6th one.

Therefore in awk you just have to test if the 5th element is equal to MATCH: and print the 6th element of the line if correct.

$ echo "2017-01-19:31:51 [ABCD:] 37723 - MATCH: 10 [text]" |awk -e '{ if ($5 == "MATCH:") print $6 }' 
    10

EDIT: Given the assumption MATCH: can be anywhere in the line:

  $ echo "2017-01-19:31:51 [ABCD:] 37723 - MATCH: 10 [text]" |awk -e '{ for (x=1; x<NF; x++ ) { if ($x == "MATCH:") {x=x+1; printf("%s\n", $x); break}}}' 
10

Might not be very elegant, but you need to iter through all fields of the line and test each field, which is done with a for loop, and an if test. If test field is matching, then print the next field.

I just added a break to directly jump to the next line and and continue the current field iteration.

On a multi line file:

$ cat terst 
2017-01-19:31:51 [ABCD:] 37723 - MATCH: 10 [text]
2017-01-19:31:51 [ABCD:] 37723 - MATCH: 11 [text]
2017-01-19:31:51 [ABCD:] 37723 - [text]
2017-01-19:31:51 37723 - MATCH: 12 [text]
$ awk -e '{ for (x=1; x<NF; x++ ) { if ($x == "MATCH:") {x=x+1; printf("%s\n", $x); break}}}' terst 
10
11
12
Source Link
netmonk
  • 1.9k
  • 1
  • 15
  • 20

Given the assumption that all your lines are formated the same (or at least all the lines containing MATCH:), it appears that MATCH: is the 5th element of the line, and the value you want is the 6th one.

Therefore in awk you just have to test if the 5th element is equal to MATCH: and print the 6th element of the line if correct.

$ echo "2017-01-19:31:51 [ABCD:] 37723 - MATCH: 10 [text]" |awk -e '{ if ($5 == "MATCH:") print $6 }' 
    10