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
1
  • 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. Commented Nov 14, 2017 at 4:26

2 Answers 2

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
1
  • pr is the right tool. You don't need sed: just add -T and -s options to pr: pr -2Ts file Commented Nov 14, 2017 at 14:36
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,

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.