0

I need to fetch elapsed time output from file. I need the value just before "elapsed" that is 2:10:42.

File content :

312.90user 15.57system 2:10:42elapsed 4%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (1major+152440minor)pagefaults 0swaps
  • 2
    That looks like GNU time output. You could use time --format=%e then – Stéphane Chazelas Apr 20 '17 at 18:36
  • If any of the existing answers solves your problem, please Accept it with the checkmark next to it. Thank you! – Jeff Schaller May 13 '17 at 2:11
0

Relatively easy with Perl:

perl -n -E '/([0-9:]+)elapsed/ and say $1' «FILE»

Note that just looks for a time-like thing before the word "elapsed" (without any spaces). That may suffer from a false-positive if applied to other files. Also, it'll check each line in the file (and print any elapsed time found), not just the first.

0

On lines that contain "(time)elapsed", search for "(anything) (space) (time)elapsed(anything)" and replace everything with "(time)", and then print the resulting line:

sed -n '/[0-9:]*elapsed/ { s/.* \([0-9:]*\)elapsed.*/\1/;p }' input > output
0

Just do this:

Assuming your data is in a file named file you can do this:

$ head -1 file | awk '{ print $3 }' | sed 's/elapsed//'

And it will return: 2:10:42


Breaking it down:

Grab the first line of your file with:

head -1 file

The heavy lifting is done by awk:

awk '{ print $3 }'

which just prints the third data block 2:10:42elapsed.

Then use the sed string editor to tidy up:

sed 's/elapsed//'

You can redirect the output anywhere you like, for example, just add:

> output.text

to output 2:10:42 to a file named output.txt

The above will clobber (replace) any content in the output.txt file. If you want to append your data to the end of your file instead just use >> instead of >.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.