0

If I run time uname -a | awk '{ print substr($3,1,9)" "$12 }', I get:

$ time uname -a | awk '{ print substr($3,1,9)" "$12 }'
4.15.0-70 x86_64

real    0m0.016s
user    0m0.003s
sys     0m0.016s
$ 

I would like to run the above command several times and save the entire output to a file. So I tried

for i in {1..10}; do time uname -a | \
awk '{ print substr($3,1,9)" "$12 }' >> uname.txt; done

But only 4.15.0-70 x86_64 is sent ten times to the file uname.txt. The output of time is sent to the screen for each run and not to uname.txt.

So how can I have the output of time also captured in uname.txt?


I can use script and that works but the output isn't pretty:

(B[m(B[m(B[m4.15.0-70 x86_64

real    0m0.017s
user    0m0.013s
sys 0m0.007s
(B[m(B[m(B[m4.15.0-70 x86_64

real    0m0.015s
user    0m0.008s
sys 0m0.009s
(B[m(B[m(B[m4.15.0-70 x86_64

real    0m0.015s
user    0m0.008s
sys 0m0.008s

Or as an image:

Part of script output as seen in Geany

I know I could clean it up if there's no better way.

1
  • problem is time command; even alone, you won't get lines expected
    – damadam
    Dec 13, 2019 at 12:09

1 Answer 1

2

You can try the following:

for i in {1..10}; do 
  { time uname -a | awk '{ print substr($3,1,9)" "$12 }'; } &>> uname.txt
done

Output:

4.15.0-54 i686

real    0m0,009s
user    0m0,007s
sys 0m0,000s
4.15.0-54 i686

real    0m0,016s
user    0m0,005s
sys 0m0,004s
4.15.0-54 i686

...
4
  • 1
    What's the value of using a variable here? why not simply do { time uname -a | awk '{ print substr($3,1,9)" "$12 }'; } >> uname.txt 2>&1 Dec 13, 2019 at 13:10
  • 1
    @steeldriver updated the answer with your recommendation. Dec 13, 2019 at 13:19
  • Check whether you need just >> or &>> as used by steeldriver.
    – DK Bose
    Dec 13, 2019 at 13:20
  • @JusticeforMonica ah yes I always forget about the bash &>> redirector Dec 13, 2019 at 13:21

Not the answer you're looking for? Browse other questions tagged .