Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: Extract multiple entries from each line

I have a log file that looks somewhat like this after grep my_function $LOG_FILE:

[0] my_function took 96.78581194020808 ms
[1] my_function took 82.0779490750283 ms
[2] my_function took 187.79653799720109 ms
[1] my_function took 98.69955899193883 ms
[0] my_function took 10.296131949871778 ms[1] my_function took 2.5152561720460653 ms
[1] my_function took 2.210912061855197 ms
[2] my_function took 3.418975044041872 ms

From this file, I would like to only extract the numbers from each line. Normally, I would use awk '{print $4}' to do this, but this log contains a few lines with two entries. However, here, I sometimes need to select two separate entries from a single line. How can I appropriately select these with bash/GNU tools?

like image 664
Green绿色 Avatar asked Aug 23 '26 22:08

Green绿色


1 Answers

You appear to sometimes have a second line after the first line with the same format, where the value of interest is in the 4th column.

If that is always the case, instead of printing the 4th column, you can print every column where column_number % 4 == 0

awk '{ for (i=1; i<=NF; i++) if (i%4 == 0) print $i }' file

Output

96.78581194020808
82.0779490750283
187.79653799720109
98.69955899193883
10.296131949871778
2.5152561720460653
2.210912061855197
3.418975044041872
like image 100
The fourth bird Avatar answered Aug 27 '26 00:08

The fourth bird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!