You are not logged in.

#1 2012-05-18 14:26:56

kdar
Member
From: Sweet home Alabama!
Registered: 2009-06-27
Posts: 356

grep - filter twice?

I want to be able to filter time output so that I would only output numbers for real time.

Typical output of time is :

user@user-VirtualBox:~$ time gzip -fc -1 rafale.bmp > rafale.bmp.gz

real	0m0.331s
user	0m0.156s
sys	0m0.060s

By using grep, to filter for "real", I get this:

armen@armen-VirtualBox:~$ (time gzip -fc -1 rafale.bmp > rafale.bmp.gz) 2>&1 | grep real
real	0m0.280s

How can I get only the 0.280? without "real"

Last edited by kdar (2012-05-18 14:27:28)

Offline

#2 2012-05-18 14:35:59

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: grep - filter twice?

4th item for this google search.

Scott

Offline

#3 2012-05-18 14:38:16

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,556
Website

Re: grep - filter twice?

 <your command> | awk '/real/ {print $2}'

Or if you want to also drop the minutes, the 'm' and the 's':

<your command> | awk '/real/ {gsub(/0m/,"",$2);gsub(/s/,"",$2);print $2}'

Last edited by Trilby (2012-05-18 14:40:14)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2012-05-18 14:42:33

roentgen
Member
Registered: 2011-03-15
Posts: 91

Re: grep - filter twice?

firecat53 wrote:

4th item for this google search.

It's second item for me wink. Google isn't as reliable as you try to imply.

Offline

#5 2012-05-18 14:48:42

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: grep - filter twice?

This'll cut the 0m0 but not the 's' - you'll need gsub as shown by Trilby for that (I think).

<your command> | awk -Fm '/real/ {print $2}'

Last edited by skanky (2012-05-18 14:49:14)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#6 2012-05-18 14:53:53

kdar
Member
From: Sweet home Alabama!
Registered: 2009-06-27
Posts: 356

Re: grep - filter twice?

Thanks everyone.

Does anyone know if I should avoid using time -p? Will it give different results than time?
help time states that -p prints the timing summary in the portable Posix format, however, will the result differ from those of time (without using -p)?

Offline

#7 2012-05-18 16:10:10

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,556
Website

Re: grep - filter twice?

I don't know much about time ... just ask anyone who's scheduled meetings I'm supposed to show up to! ... ;o)

But you could try both.  It seems to me the '-p' drops the minutes, the 'm' and the 's'.  So if you just want a clean number, use the '-p' options and use the simpler awk command.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2012-05-18 20:07:31

kdar
Member
From: Sweet home Alabama!
Registered: 2009-06-27
Posts: 356

Re: grep - filter twice?

well, if it just for reformatting output, I guess this is ok. But I worry if it calculates real time somehow different with -p.

Offline

Board footer

Powered by FluxBB