Saturday, June 22, 2013

Extract decimals from text using grep

Let's say you have a log file that looks like this:
I/Profiling: TimeInterval1:  3271.72 ms
I/Profiling: TimeInterval1:  3379.15 ms
I/Profiling: TimeInterval1:  3645.32 ms
...
and you want to extract all decimal numbers so that you can get the average, or plot them. Here's how you would use grep to do this:
grep -o ' [0-9]\+\.[0-9]\+' mylogfile.log
The -o option is for just printing the matching text and not the whole line.