The answer of @kraxor uses sed
to remove "everything else" from the line, and then just show the line.
But - what actually is "everything else"? Here we have an idea at least, from one sample line. But what when we get a CSV header line? What to remove?
In general, we don't know.
Therefore, it is better to actually show the data we want to show!
echo '345,0m0.047s' |
sed -n -r 's/^(.*),.*[^0-9]([0-9]*)\.(.*)s$/\1,\2\3/p'
345,0047
Works so far!
What does it do?
We do not print the line normaly (-n
)
(and activate the nicer "extended regular expressions" (-r
))
now look for the ID, second, and fractional second,
and if we found them, put them into the line, in the right format (adding ","),
and print the newly created line.
Now some other input, pretty normal, with two lines of data:
ID,execution_time
123, Oops a comment0m0.0333s
345,0m0.047s
Huh?! Looks like real world data, actually!
echo "ID,execution_time\n123, Oops a comment0m0.0333s\n345,0m0.047s" |
sed -r -n 's/^(.*),.*[^0-9]([0-9]*)\.(.*)s$/\1,\2\3/p'
123,00333
345,0047
Looks nice and just right!
To show that it has some merit to do it this way, I'll compare to the earlier answer:
echo "ID,execution_time\n123, Oops a comment0m0.0333s\n345,0m0.047s" |
sed 's/[0-9]*m\|s\|\.//g'
ID,execution_tie
123, Oop a coent00333
345,0047
Ok, the actual, clean data lines went through nicely;
But the other parts, not so much (note the header was not just passed, but cut off.).
(Note there is a solution to explicitly skip the header line (or maybe the first data line, do we know?))