How to grep two numbers from the same line at different places using bash?
Solution 1:
One possible way...
% grep 'solver.cpp:229' ExampleFile.txt | cut -d ' ' -f 3,6 | tr -d ','
2000 0.305721
2020 0.294722
Solution 2:
I lost grep
but here it is with sed
$ sed -nr 's/.*Iteration ([0-9]+).*loss.*( [0-9]+.*)/\1\2/p' ExampleFile.txt
2000 0.305721
2020 0.294722
-
-n
don't print until we ask for something -
-r
use ERE so I don't have to escape the()
and+
metacharacters -
s
search and replace/old/new/
-
.*
matches any (or no) characters -
([0-9]+)
parentheses to keep this part of the pattern[0-9]
a number+
one or more occurrences of the preceding character. -
\1\2
backreferences to the patterns saved earlier with parentheses -
p
print the bits we want to see
If the output is what you want, redirect it to your outfile:
sed -nr 's/.*Iteration ([0-9]+).*loss.*( [0-9]+.*)/\1\2/p' ExampleFile.txt > ResultFile.txt
Solution 3:
With awk specify Field separator as ',' comma and 'space' and match those lines which contain "Iteration" in, next print the columns #3 and #7 (or $NF as last column instead of $7)
awk -F'[, ]' '/Iteration/ {print $3,$7}' infile