klapsak
klapsak

Reputation: 15

Print specific number from string via AWK/SED

I have large file from which I need to extract specific value. I am looking for the string with "Brontes" word in it and for the time of the service start.

2016-09-05 11:23:08,022 ERROR [xxx.xxx.xx] (MSC service thread 1-6) XXXX015875: XXXX AS 7.1.1.Final "Brontes" started (with errors) in 275637ms - Started 1221 of 1307 services (3 services failed or missing dependencies, 71 services are passive or on-demand)

I was trying to print this like that:

awk '/Brontes/{print $18}' file

and result is correct: 275637ms

But when the string looks slightly different (amount of columns is changed) the result will not be the same.

How do I achieve same outcome but in the way that it is not dependant on the columns amount ?

Upvotes: 0

Views: 42

Answers (2)

P....
P....

Reputation: 18371

Sample input:

echo $ola
2016-09-05 11:23:08,022 ERROR [xxx.xxx.xx] (MSC service thread 1-6) XXXX015875: XXXX AS 7.1.1.Final Brontes started (with errors) in 275637ms - Started 1221 of 1307 services (3 services failed or missing dependencies, 71 services are passive or on-demand)

Solution using awk: Scan through each column and print the column which contains ms in it.

echo $ola |awk '/Brontes/{for(i=1;i<=NF;i++) if( $i ~ /[0-9]+ms/) print $i}'
275637ms

Upvotes: 1

Sundeep
Sundeep

Reputation: 23667

grep -oP 'Brontes.* \K\d+ms' file

This prints all numbers ending with ms from a line containing the string Brontes prior to that number

Upvotes: 1

Related Questions