Formatting input data

Hello everybody,

I have a file containing some statistics regarding CPU usage. The file has this syntax :

Fri Jul 16 14:27:16 EEST 2010
Cpu(s): 15.2%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 15:02:17 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 15:21:02 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 15:54:43 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 16:20:15 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 16:40:18 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.1%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 17:00:19 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 17:20:22 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st

meaning a line representing the date, followed after by a line with CPU stats, and so one.
What I intend to do is to format this file in another one, which will contain only 2 columns :

Col A   Col B
Hour    CPU(user)

like this :

14:27:16  15.2%
15:02:17  15.3%

Does anyone know a solution to this ?

Thanx,
Adrian

Try this:

awk 'dump(s1,s2){printf("%20s %20s\n", s1, s2)}
     BEGIN{ dump("Col A", "Col B"); 
               dump("Hour","CPU(user)");
          }
     { if(NR%2) {
          printf("%20s", $3)
       }
       else {
          printf("%20s\n", $3)
       }  }'   inputfile > outputfile
        

It gives me this error :

 unexpected newline or end of string

at line 10

Another approach:

awk -F"[ %]" '/Cpu/{print t, $2 "%"}{t=$4}'  file

Still this isn't doing what I want :frowning:

This is my output with the given input file:

$ cat file
Fri Jul 16 14:27:16 EEST 2010
Cpu(s): 15.2%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 15:02:17 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 15:21:02 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 15:54:43 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 16:20:15 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 16:40:18 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.1%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 17:00:19 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
Fri Jul 16 17:20:22 EEST 2010
Cpu(s): 15.3%us,  1.4%sy,  0.0%ni, 82.3%id,  0.1%wa,  0.0%hi,  0.9%si,  0.0%st
$
$ awk -F"[ %]" '/Cpu/{print t, $2 "%"}{t=$4}'  file
14:27:16 15.2%
15:02:17 15.3%
15:21:02 15.3%
15:54:43 15.3%
16:20:15 15.3%
16:40:18 15.3%
17:00:19 15.3%
17:20:22 15.3%
$

Hi.

The solution from Franklin52 seems to give exactly what you want.

$ awk -F"[ %]" '/Cpu/{print t, $2 "%"}{t=$4}' file1
14:27:16 15.2%
15:02:17 15.3%
15:21:02 15.3%
15:54:43 15.3%
16:20:15 15.3%
16:40:18 15.3%
17:00:19 15.3%
17:20:22 15.3%

Please elaborate on "isn't doing what I want". How so, exactly?

Sorry, it is working alright, my bad :o. Thx guys :b:

---------- Post updated 07-17-10 at 10:38 AM ---------- Previous update was 07-16-10 at 01:24 PM ----------

Hey guys, it seems that I was right. When I first tested the command, my CPU was not so solicited , so it was at 5.0% .When I rechecked , it was at a grater value, 60%. It seems that when it is formed from 1 digit value, the output is bad ( it prints % on the second column). Have an idea how to fix this ?

Hi.

This is based on the same data...

$ awk '/Cpu/{sub( /%.*/, "", $2 ); print t, $2 "%"}{t=$4}'  file
14:27:16 15.2%
15:02:17 15.3%
15:21:02 15.3%
15:54:43 15.3%
16:20:15 15.3%
16:40:18 15.3%
17:00:19 15.3%
17:20:22 15.3%

Thx scottn, this works perfectly :cool: