简体   繁体   中英

using sed to extract a time from a line

line:

0.01user 0.00system 0:13.46elapsed 0%CPU (0avgtext+0avgdata 4272maxresident)k

I want to grab:

0:13.46

my current regex is:

sed 's/.*\([0-9]*:[0-9]*.[0-9]*\)elapsed.*/\1/'

I'm pretty sure the regex is correct, but it's not finding anything. It's probably something really simple, I'm just doing 10 things at once.

Why not use awk?

awk '/elapsed/ {gsub(/elapsed/,""); print $3}'

I tried your sed expression on SL and got :13.46

This works for me:

% sed 's/.*\s\([0-9]*:[0-9]*.[0-9]*\)elapsed.*/\1/'
0.01user 0.00system 0:13.46elapsed 0%CPU (0avgtext+0avgdata 4272maxresident)k
0:13.46

Note how I put a \\s before the first digit you want to match.

Then again, your regex kind of worked for me before in that it would print out :13.46 (the .* gobbled up the first 0 that you want to print out).

I got :13.46 . This is caused by the initial .* which expands as much as possible without compromising the following [0-9]*: .

My suggestion: sed 's/.*system //;s/elapsed.*//'

If you have a version of time that supports the format option, you don't need to parse its output:

$ /usr/bin/time -f %E sleep 2.27
0:02.27

In Bash, using its builtin time :

$ TIMEFORMAT=%R
$ time sleep 2.27
2.337

Here's another solution:

echo $word | grep -oP '[^ ]*(?=elapsed)'

This looks for the largest character string before 'elapsed' which does not contain a space character.

This looks like a problem for which awk is better suited. You can solve this in awk without using regular expressions which are (relatively) inefficient.

awk '{ print substr($3, 1, length($3) - 7) }'

In a test I just ran, this awk solution is an order of magnitude faster than the accepted sed solution. I believe the significant performance improvement comes from avoiding regular expressions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM