Formatting input data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting input data
# 1  
Old 07-16-2010
Question Formatting input data

Hello everybody,

I have a file containing some statistics regarding CPU usage. The file has this syntax :
HTML Code:
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 :
HTML Code:
Col A   Col B
Hour    CPU(user)
like this :
HTML Code:
14:27:16  15.2%
15:02:17  15.3%
Does anyone know a solution to this ?

Thanx,
Adrian
# 2  
Old 07-16-2010
Try this:
Code:
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


Last edited by jim mcnamara; 07-16-2010 at 07:27 PM.. Reason: correct syntax error
# 3  
Old 07-16-2010
It gives me this error :
HTML Code:
 unexpected newline or end of string
at line 10
# 4  
Old 07-16-2010
Another approach:
Code:
awk -F"[ %]" '/Cpu/{print t, $2 "%"}{t=$4}'  file

# 5  
Old 07-16-2010
Still this isn't doing what I want Smilie
# 6  
Old 07-16-2010
Quote:
Originally Posted by spiriad
Still this isn't doing what I want Smilie
This is my output with the given input file:
Code:
$ 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%
$

# 7  
Old 07-16-2010
Quote:
Originally Posted by spiriad
Still this isn't doing what I want Smilie
Hi.

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

Code:
$ 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?

Last edited by Scott; 07-16-2010 at 06:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Data formatting using awk

Need assistance on the data extraction using awk Below is the format and would like to extract the data in another format ------------------------------------------------------------------------------------------------- Minimum Temperature (deg F ) DAY 1 2 3 4 5 6 7 8 9 10 11... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

2. Shell Programming and Scripting

Help with data formatting

Hi, I am generating the following output from my script. Country,A,B,C,D,E,F INDIA ,3755019,774604,484749,329838,7333612,442031 CHINA ,3716520,889197,530899,379754,6198475,355768 JAPAN ,52038,30462,231224,147275,1272,498 USA,9494,1130,0,0,15303,451... (5 Replies)
Discussion started by: karumudi7
5 Replies

3. Shell Programming and Scripting

Input handling and formatting input to shell

i want to get input and depending on it create new commands for input to expect. But problem is that after giving date or month as 01-09 it is interpretation as 1-9 echo -n "ENTER DATE " read d1 echo -n "ENTER MONTH " read m1 echo -n "ENTER YEAR" read y1 o=1 i=1 d2=`expr $d1... (1 Reply)
Discussion started by: sagar_1986
1 Replies

4. Shell Programming and Scripting

Formatting and combining fields of the input file

Hi, I have a file of the following format: AV 103 AV 104 AV 105 AV 308 AV 517 BN 210 BN 211 BN 212 BN 218 and the desired output is : AV 103-105 3 AV 308 1 AV 517 1 BN 210-212 3 (5 Replies)
Discussion started by: rochitsharma
5 Replies

5. UNIX for Advanced & Expert Users

formatting the data

HI I want to make it single row if start with braces i.e. { .Any idea {1:XXX2460275191}{2:SEC00687921131112201641N}{3:{58910}}{4: :R:GENL :C::xx//xx1 :20C::yy//yy1 :2S:xxT} {1:XXX2460275190}{2:SEC00687921131112201641y}{3:{58911}}{4: :z:GENL :v::xx//xx1 :10C::yy//yy1 :4S:xxT ... (2 Replies)
Discussion started by: mohan705
2 Replies

6. Shell Programming and Scripting

help with data formatting

Hi, I have data coming in like below. Not all data is like that, these are the problem records that is causing the ETL load to fail. Can you pls help me with combining theese broken records! 001800018000000guyMMAAY~acct name~acct type~~"address part 1 address... (8 Replies)
Discussion started by: varman
8 Replies

7. Shell Programming and Scripting

How to check field formatting of input file?

Hi, I had input file with below data, abcdefghij;20100903040607;1234567891;GLOBAL; Having values of fields with seperated by semi-colon (;) and ended with line feed (\n). Through shell script, how can I check the field formatting? Thanks in advance. (18 Replies)
Discussion started by: Poonamol
18 Replies

8. Shell Programming and Scripting

Script for data formatting

Hi I have to convert the data in a file ******* 01-20-09 11:14AM 60928 ABC Valuation-2009.xls 01-20-09 11:16AM 55808 DEF GHI Equation-2009.xls 01-20-09 11:02AM 52736 ABC DF Valuation-2009.xls 01-20-09 11:06AM 89600 THE... (6 Replies)
Discussion started by: shekhar_v4
6 Replies

9. UNIX for Dummies Questions & Answers

Formatting Data

i am writing a script that reads in a file and i just want it to print each element on a new line here is my code and the data that i want to read in #!/usr/bin/perl use strict; use CGI qw(:standard); use CGI qw(:cgi); my $data_file = "/tmp/results.txt"; my $configuration; my... (3 Replies)
Discussion started by: nmeliasp
3 Replies
Login or Register to Ask a Question