0

I have a cronjob that run monthly to generate some report from our system. However, the current output is as below:

Uptime
100
45
50
Average CPU
2.36
15.6
70.8

How can I generate the output into a CSV file in a table format, something like

Uptime        Average CPU
100            2.36
45            15.6
50            70.8
  • 2
    (1) Is the objective to create a table, with columns separated by spaces and/or tabs, so you can take that and make it into a CSV, or is the objective to create a CSV?  It would be just as easy to go directly from your monocolumn data to CSV.  (2) Just out of curiosity, how would you convert the table to the CSV?   Please do not respond in comments; edit your question to make it clearer and more complete. – G-Man Says 'Reinstate Monica' Nov 14 '17 at 4:26
2

Using the rs (reshape) command: given

$ cat output 
Uptime
100
45
50
Average CPU
2.36
15.6
70.8

then

$ rs -et 0 2 < output 
Uptime       Average CPU
100          2.36
45           15.6
50           70.8

You can change the output delimiter with -C e.g.

$ rs -et -C, 0 2 < output 
Uptime,Average CPU,
100,2.36,
45,15.6,
50,70.8,
3

Use below command:

pr -2 l.txt  | sed '/^$/d' | sed -r "s/\s+/ /g" |sed '1d' 

l.txt contains the text which you have mentioned in post. Output:

$ pr -2 l.txt  | sed '/^$/d' | sed -r "s/\s+/ /g" |sed '1d'
Uptime                  Average CPU
100                 2.36
45                  15.6
50                  70.8
  • pr is the right tool. You don't need sed: just add -T and -s options to pr: pr -2Ts file – glenn jackman Nov 14 '17 at 14:36

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.