Convert rows into column along with header


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert rows into column along with header
# 1  
Old 05-01-2017
Convert rows into column along with header

Hi,

I have a requirement to format the data in a new order. Here is my source format :

Code:
ppp ***Wed Dec 16 10:32:30 GMT 2015
header1 header2 header3 header4 header5

server1  0.00    0.02     0.07   0.98
server2  0.01    0.00     0.08  0.79 
server3  0.05    0.82     0.77  0.86

I need to change the above data snippet to the following format.
Code:
ppp ***Wed Dec 16 10:32:30 GMT 2015 server1-header2 server1-header3 server1-header4 server1-header5 server2-header2 server2-header3 server2-header4 server2-header5 server3-header2 server3-header3 server3-header4 server3-header5

0.00    0.02      0.07  0.98 0.01  0.00  0.08 0.79 0.05    0.82   0.77    0.86

The intent is to create multiple tables of timestamp while converting rows into columns.

I am not sure from to start, any help. I tried awk to grab the column, and not sure how to merge rows into column.

Thanks in advance.
# 2  
Old 05-01-2017
Not trying to be unhelpful: but the answer is already at the lower left hand corner.
These are search results so you don't have to wait for someone to answer.

This is one of the most common questions, you may have to read a few answers to get one you like.
# 3  
Old 05-01-2017
Hi Jim,

I looked at those results, but do not find any similarities.

First thing seems complex is how to merge headers, while having data mapped.
# 4  
Old 05-01-2017
Try
Code:
awk '
NR == 1         {printf "%s ", $0
                 next
                }
NR == 2         {for (i=2; i<=NF; i++) HD[i] = $i
                 MXHD = NF
                 next
                }

                {MXSV     = NR - 3
                 SV[MXSV] = $1
                 for (i=2; i<=NF; i++) DT[i, MXSV] = $i
                }
END             {for (i=1; i<=MXSV; i++)
                   for (j=2; j<=MXHD; j++) printf " %s-%s", SV[i], HD[j]
                 printf RS 
                 for (i=1; i<=MXSV; i++)
                   for (j=2; j<=MXHD; j++) printf "%s\t", DT[j,i]
                 printf RS 


                }
' file
ppp ***Wed Dec 16 10:32:30 GMT 2015  server1-header2 server1-header3 server1-header4 server1-header5 server2-header2 server2-header3 server2-header4 server2-header5 server3-header2 server3-header3 server3-header4 server3-header5
0.00    0.02    0.07    0.98    0.01    0.00    0.08    0.79    0.05    0.82    0.77    0.86

# 5  
Old 05-02-2017
Thanks Rudic.

This seems to work. Appreciate if you can elaborate a bit. Being a biologist I do not use awk much, and to me this look pretty complex.
# 6  
Old 05-02-2017
"seems to work" is not quite the expected result...

Code:
awk '
NR == 1         {printf "%s ", $0                                               # print time stamp
                 next                                                           # continue with next record
                }
NR == 2         {for (i=2; i<=NF; i++) HD[i] = $i                               # collect header names from second field
                 MXHD = NF                                                      # collect header count
                 next                                                           # continue with next record
                }
                {MXSV     = NR - 3                                              # determine & keep maximum server No.
                 SV[MXSV] = $1                                                  # collect server name
                 for (i=2; i<=NF; i++) DT[i, MXSV] = $i                         # collect server data
                }
END             {for (i=1; i<=MXSV; i++)                                        # use nested loops (i & j)
                   for (j=2; j<=MXHD; j++) printf " %s-%s", SV[i], HD[j]        # to print server-header combinations
                 printf RS                                                      # print line feed
                 for (i=1; i<=MXSV; i++)                                        # use nested loops (i & j)
                   for (j=2; j<=MXHD; j++) printf "%s\t", DT[j,i]               # to print server data
                 printf RS                                                      # print line feed
                }
' file

# 7  
Old 05-02-2017
I mentioned seems to work because now I am trying with real data, and I see that it misses the first header ( ie vda ).

Code:
zzz ***Wed Dec 16 10:32:30 GMT 2015
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          12.23    0.00    7.45   76.06    0.00    4.26
 
Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
vda               0.00     0.00    0.00    2.00     0.00     4.00     4.00     0.24  128.00    0.00  128.00 121.00  24.20
vdb               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
vdc               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
vdd               0.00     0.00    2.00    1.00     1.00     0.50     1.00     0.11   36.67   33.00   44.00  36.67  11.00
vde               0.00     0.00    2.00    1.00     1.00     0.50     1.00     0.04   14.67    0.00   44.00  14.67   4.40
vdf               0.00     0.00    2.00    1.00     1.00     0.50     1.00     0.10   31.67   25.50   44.00  31.67   9.50
vdg               0.00     0.00   43.00   23.00   400.00    97.00    15.06     3.83   57.05   62.37   47.09  15.15 100.00
vdh               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
dm-0              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
dm-1              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.24    0.00    0.00    0.00   0.00  24.20
dm-2              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
vdi               0.00     0.00   39.00   23.00   320.00    78.50    12.85     1.11   17.40    2.85   42.09  12.39  76.80

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Column data values to rows

Hi all , I have a file with the below content Header Section employee|employee name||Job description|Job code|Unitcode|Account|geography|C1|C2|C3|C4|C5|C6|C7|C8|C9|Csource|Oct|Nov|Dec|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep Data section ... (1 Reply)
Discussion started by: Hypesslearner
1 Replies

2. Shell Programming and Scripting

Convert rows to column and add header

Hi, I need help to convert rows in input file into a table. inputfile 192.98.1 192.98.192.98.17 VVC family Zorro 10 192.98.1 192.98.192.98.17 VVC family Ace 1 192.98.1 192.98.192.98.17 VVC family ... (4 Replies)
Discussion started by: redse171
4 Replies

3. Shell Programming and Scripting

Convert single column into multiple rows

Convert Single column to multiple rows file a.txt contains data like below Server=abc Run=1 Tables=10 Sessions=16 Time=380 Jobs=5 Server=abc Run=2 Tables=15 Sessions=16 Time=400 Jobs=5 Server=abc Run=3 Tables=20 Sessions=16 Time=450 (5 Replies)
Discussion started by: sol_nov
5 Replies

4. Shell Programming and Scripting

Convert header rows into

I want to put the 3 first lines into a single line separated by ; I've tried to use Sed and Awk but without success. I'm new to Shell scripting. Thanks in advance! Input 112 DESAC_201309_OR_DJ10 DJ10 1234567890123;8 1234567890124;20 1234567890125;3 expected Output... (8 Replies)
Discussion started by: MoroccanRoll
8 Replies

5. Shell Programming and Scripting

Convert Rows into Column

Hi Experts, I have a requirement to convert rows into columns. For e.g. Input File: Output File should be like Appreciate if you could suggest code snippet(may be awk) for above requirement... Thanks in Advance for your help... (3 Replies)
Discussion started by: sai_2507
3 Replies

6. Shell Programming and Scripting

Convert rows into column groups

Hi I have the text file like this "A" "AA Info" "AA Text" "AAA" "ABC" "ABC Info" "ABC Tech" "AGH" "SYN" "SYMBony" "SYN BEREN" Like about 2000 lines Output would be in Column with groups like following "A" "AA Info", "AA Text" "AAA" "ABC","ABC Info","ABC Tech" (0 Replies)
Discussion started by: selvanraj
0 Replies

7. Shell Programming and Scripting

Convert Column to rows

Hi, I have a file with below contents. Heading1 Heading2 Heading3 Heading4 Value1 Value2 Value3 Value4 The file has only 2 rows and is tab separated The desired output is : Heading1 Value1 Heading2 Value2 Heading3 Value3 Heading4 Value4 CAn you please help? (5 Replies)
Discussion started by: kaponeh
5 Replies

8. UNIX for Dummies Questions & Answers

How to convert a single column into several rows and columns?

I have a program which gives me the output as a single column with hundreds of rows like: 213 314 324 324 123 I want to be able to create a new file from this file which allows me to set the number of rows and columns in the new file, i.e. for this example, if I specify 3 rows and 2... (5 Replies)
Discussion started by: ashton_smith
5 Replies

9. Shell Programming and Scripting

convert rows into column

if u have a data 2 4 6 8 5 4 4 5 6 then result shud be like 2 4 6 7 5 4 4 5 6 (3 Replies)
Discussion started by: cdfd123
3 Replies

10. Shell Programming and Scripting

Convert Header into Column in Text file

Hi Gurus, I have a requirement like this and have to create a UX shell scripts. Thanks in advance. File-in: ------ Header2007-12-012007-11-21 100|xyz|was 101|wsa|qws ...... ....... Output should be: ------------------- 2007-12-01|100|xyz|was 2007-12-01|101|wsa|qws ...... .......... (7 Replies)
Discussion started by: vsubbu1000
7 Replies
Login or Register to Ask a Question