How to extract a field which has value greater than a number using AWK or SED

Viewed 376

Sorry for such a basic question, but I have spent hours trying to work this out! I need an awk command (or similar) that will look at a text file and output to the screen if the 12th field of each line has a value greater than or equal to x.

In this below example I would like to get number of transactions exceeded 950ms

INFO [tomcat-http--25] 2020-02-21 12:33:55,292 - Tranasction task has exceeded 100ms @ 1102ms for [ unique_id1:9502, unique_id2:995351, Serialnumber:514534, Txnid:70828febbb1913 ]
2 Answers

From gawk docs:

To force a string to be converted to a number, add zero to that string. A string is converted to a number by interpreting any numeric prefix of the string as numerals: "2.5" converts to 2.5, "1e3" converts to 1,000, and "25fix" has a numeric value of 25. Strings that can’t be interpreted as valid numbers convert to zero.

So the best solution will be:

awk -v ms=950 '$12+0>ms+0' your_file

-v ms=950: Will pass a var named ms to the body of the awk program

Where @JuanDiegoGodoyRobles (++) played it safe, I'm boldly going with:

$ awk -v ms=950ms '$12+0 > ms+0' file

or:

$ awk -v ms=666 '$12+0 > ms+0' file

Output:

INFO [tomcat-http--25] 2020-02-21 12:33:55,292 - Tranasction task has exceeded 100ms @ 1102ms for [ unique_id1:9502, unique_id2:995351, Serialnumber:514534, Txnid:70828febbb1913 ]
Related