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
# 15  
Old 05-03-2017
Hi Rudic,

This looks excellent.

I have two minor changes.

1. I need to print header only once.
2. Also, there should be a new first column "Timestamp" where timestamp value will be place like this "*Wed Dec 16 10:33:30 GMT 2015" , I mean no zzz *** only timestamp.

Here is the sample format :

Code:
Timestamp vda-rrqm/s vda-wrqm/s vda-r/s vda-w/s vda-rkB/s vda-wkB/s vda-avgrq-sz vda-avgqu-sz vda-await vda-r_await vda-w_await vda-svctm vda-%util vdb-rrqm/s vdb-wrqm/s vdb-r/s vdb-w/s vdb-rkB/s vdb-wkB/s vdb-avgrq-sz vdb-avgqu-sz vdb-await vdb-r_await vdb-w_await vdb-svctm vdb-%util vdc-rrqm/s vdc-wrqm/s vdc-r/s vdc-w/s vdc-rkB/s vdc-wkB/s vdc-avgrq-sz vdc-avgqu-sz vdc-await vdc-r_await vdc-w_await vdc-svctm vdc-%util vdd-rrqm/s vdd-wrqm/s vdd-r/s vdd-w/s vdd-rkB/s vdd-wkB/s vdd-avgrq-sz vdd-avgqu-sz vdd-await vdd-r_await vdd-w_await vdd-svctm vdd-%util vde-rrqm/s vde-wrqm/s vde-r/s vde-w/s vde-rkB/s vde-wkB/s vde-avgrq-sz vde-avgqu-sz vde-await vde-r_await vde-w_await vde-svctm vde-%util vdf-rrqm/s vdf-wrqm/s vdf-r/s vdf-w/s vdf-rkB/s vdf-wkB/s vdf-avgrq-sz vdf-avgqu-sz vdf-await vdf-r_await vdf-w_await vdf-svctm vdf-%util vdg-rrqm/s vdg-wrqm/s vdg-r/s vdg-w/s vdg-rkB/s vdg-wkB/s vdg-avgrq-sz vdg-avgqu-sz vdg-await vdg-r_await vdg-w_await vdg-svctm vdg-%util vdh-rrqm/s vdh-wrqm/s vdh-r/s vdh-w/s vdh-rkB/s vdh-wkB/s vdh-avgrq-sz vdh-avgqu-sz vdh-await vdh-r_await vdh-w_await vdh-svctm vdh-%util dm-0-rrqm/s dm-0-wrqm/s dm-0-r/s dm-0-w/s dm-0-rkB/s dm-0-wkB/s dm-0-avgrq-sz dm-0-avgqu-sz dm-0-await dm-0-r_await dm-0-w_await dm-0-svctm dm-0-%util dm-1-rrqm/s dm-1-wrqm/s dm-1-r/s dm-1-w/s dm-1-rkB/s dm-1-wkB/s dm-1-avgrq-sz dm-1-avgqu-sz dm-1-await dm-1-r_await dm-1-w_await dm-1-svctm dm-1-%util dm-2-rrqm/s dm-2-wrqm/s dm-2-r/s dm-2-w/s dm-2-rkB/s dm-2-wkB/s dm-2-avgrq-sz dm-2-avgqu-sz dm-2-await dm-2-r_await dm-2-w_await dm-2-svctm dm-2-%util vdi-rrqm/s vdi-wrqm/s vdi-r/s vdi-w/s vdi-rkB/s vdi-wkB/s vdi-avgrq-sz vdi-avgqu-sz vdi-await vdi-r_await vdi-w_await vdi-svctm vdi-%util
Wed Dec 16 10:32:30 GMT 2015 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   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     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    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   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    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    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  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    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    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   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    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

I regret for re-work as I misunderstood the requirement.

Thanks in advance.
# 16  
Old 05-03-2017
How about applying what you learned so far trying to bring up a solution of your own. People in here will be glad to help when you get stuck.
# 17  
Old 05-04-2017
Hi Rudic,

Being a biologist, I do get to work on awk very often, since this is typically a complex requirement, I thought of giving it a try.

I tried with headers part of it, but frankly not able to follow the entire code.

Appreciate your help so far.

Thanks.
# 18  
Old 05-04-2017
Try
Code:
awk '
function PRT()  {if (!HDDONE)   {printf "TimeStamp"
                                 for (i=1; i<=MXSV; i++)
                                   for (j=2; j<=MXHD; j++) printf "\t%s-%s", SV[i], HD[j]
                                 printf RS
                                 HDDONE = 1
                                }
                 printf "%s", TS
                 for (i=1; i<=MXSV; i++)
                   for (j=2; j<=MXHD; j++) printf "\t%s", DT[j,i]
                 printf RS 
                }

/^zzz/          {if (NR > 1) PRT()

                 split ("", HD)
                 split ("", SV)
                 split ("", DT)
                 START = 0

                 sub (/^zzz *\** */, "")
                 TS    = $0
                 next
                }

!NF             {if (START) next

                 getline
                 for (i=2; i<=NF; i++) HD[i] = $i
                 MXHD = NF
                 START = 1 
                 STNR  = NR 
                 next
                }
START           {MXSV     = NR - STNR
                 SV[MXSV] = $1
                 for (i=2; i<=NF; i++) DT[i, MXSV] = $i
                }
END             {PRT()
                }
' file
TimeStamp       vda-rrqm/s      vda-wrqm/s      vda-r/s vda-w/s vda-rkB/s       vda-wkB/s       vda-avgrq-sz    vda-avgqu-sz      ...
Wed Dec 16 10:32:30 GMT 2015    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   ...
Wed Dec 16 10:33:30 GMT 2015    0.00    0.00    0.00    2.00    0.00    4.00    4.00  ...

Please be aware that this is NOT rock solid, but fragile, as no serious error checking is done. For instance, the order of servers is not checked - if it changes, values in columns will be wrong. Nor is their count. The count of empty lines is not checked - it starts collecting data on every other empty line.
This User Gave Thanks to RudiC For This Post:
# 19  
Old 05-05-2017
Thanks a lot!
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