Member Member - 4 months ago 8
Bash Question

Regular expression for floats on grep

I0709 13:45:33.848619 3415 upgrade_proto.cpp:1044] Attempting to upgrade input file specified using deprecated 'solver_type' field (enum)': models/model
I0709 20:02:09.081399 3415 solver.cpp:228] Iteration 101, loss = 2.25779
I0709 20:02:09.081477 3415 solver.cpp:244] Train net output #0: loss = 2.25779 (* 1 = 2.25779 loss)
I0709 20:02:09.081495 3415 sgd_solver.cpp:106] Iteration 101, lr = 0.01
I0709 20:02:20.067615 3415 solver.cpp:228] Iteration 102, loss = 2.22583
I0709 20:02:20.067740 3415 solver.cpp:244] Train net output #0: loss = 2.22583 (* 1 = 2.22583 loss)


From a file like the above, I need the numbers 2.25779 and 2.22583 in this case, and dump each one on a line on a new file.

I've tried something like:

cat file.txt | grep -P 'loss = .*[0-9]\n' > res.csv


That doesn't match anything tho and I don't know how to get more specific (only the number not the loss =).

So a:

cat res.csv


Should show the output:

2.25779

2.22583

Answer

This will work with any sed on any OS:

$ sed -n 's/.*loss = \([0-9.]*\)$/\1/p' file
2.25779
2.22583

Only GNU grep would support the options/syntax you are trying to use and no grep supports what you would need to achieve your goal of only outputting part of the matching text (that would require capture groups which grep does not have).

Comments