Display Row to Columns Pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display Row to Columns Pattern
# 1  
Old 09-03-2013
Display Row to Columns Pattern

Hi All,
I have following pattern of output

Code:
 Number of executions               = 1
 Number of compilations             = 1
 Total execution time (sec.microsec)= 0.263898
 Statement text                     = ALTER TABLE DSSSTG.SG2_MIB_MIBP04 ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE
 Number of executions               = 1
 Number of compilations             = 1
 Total execution time (sec.microsec)= 0.173239
 Statement text                     = SELECT FILE_NM,SRC_STM_ID,MSR_PRD_TP,PRV_EXTR_TMST,MSR_PRD,MSR_PRD_DT FROM DSSSTG.V_SG_CTL1 V_SG_CTL1 WHERE SRC_STM_ID = ?AND FILE_NM='DSCLQ17.DAT'
Number of executions               = 4
 Number of compilations             = 1
 Total execution time (sec.microsec)= 0.011126
 Statement text                     = SELECT partitionmap FROM sysibm.systables, sysibm.syspartitionmaps WHERE sysibm.systables.pmap_id = sysibm.syspartitionmaps.pmap_id AND sysibm.systables.name='SG_SRC_X_AR' AND sysibm.systables.creator='DSSSTG  ' FOR READ ONLY

Expected Output as following

Code:
No Exec     No Compilations       Total execution time    Statement Text
--------    ---------------        -------------------    ---------------
1              1                           0.263898                    ALTER TABLE DSSSTG.SG2_MIB_MIBP04 ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE

1              1                           0.173239                    SELECT FILE_NM,SRC_STM_ID,MSR_PRD_TP,PRV_EXTR_TMST,MSR_PRD,MSR_PRD_DT FROM DSSSTG.V_SG_CTL1 V_SG_CTL1 WHERE SRC_STM_ID = ?AND FILE_NM='DSCLQ17.DAT'

4              1                           0.011126                     SELECT partitionmap FROM sysibm.systables, sysibm.syspartitionmaps WHERE sysibm.systables.pmap_id = sysibm.syspartitionmaps.pmap_id AND sysibm.systables.name='SG_SRC_X_AR' AND sysibm.systables.creator='DSSSTG  ' FOR READ ONLY



Thank You for all your contribution.
# 2  
Old 09-03-2013
How about this:

Code:
awk '
{
  col=$0
  line=$0
  gsub(/ *= .*/, "", col)
  gsub(/^[^=]*= /, "", line)
  if (!(col in COLS)) {
      head=head OFS col
      TITLE[++cols]=col
      COLS[col]=cols
  }
  if (COLS[col]==1) ROW++
  v[ROW,COLS[col]]=line
}
END {
  print substr(head,2)
  gsub("[^"OFS"]", "-", head)
  print substr(head,2)
  for(r=1; r<=ROW; r++) {
      printf("%-*s", length(TITLE[1]), v[r, 1])
      for(c=2; c<=cols; c++)
        printf("%s%-*s", OFS, length(TITLE[c]), v[r,c])
      print ""
  }
}
' OFS='\t' infile

These 2 Users Gave Thanks to Chubler_XL For This Post:
# 3  
Old 09-03-2013
Here is another way to do it assuming that you always have 4 input lines / output line and that the input lines are always in the same relative order. It uses slightly different headings than you requested, but otherwise seems to produce output similar to what you seem to want:
Code:
#!/bin/ksh
printf "# Exec # Comps Execution time Statement Text\n%s\n" \
       "------ ------- -------------- --------------"
i=0
while IFS== read -r junk st
do      case $i in
        (0)     printf "%6s" "$st";;
        (1)     printf "%8s" "$st";;
        (2)     printf "%15s" "$st";;
        (3)     printf "%s\n" "$st";;
        esac
        i=$((++i % 4))
done < file

This was tested with the Korn shell on Mac OS X, but will work with any POSIX conforming shell (including bash and ksh).

If you want different headings and different alignment, this should at least serve as a template for one way to reformat your output.

The output produced by this script with your sample input is:
Code:
# Exec # Comps Execution time Statement Text
------ ------- -------------- --------------
     1       1       0.263898 ALTER TABLE DSSSTG.SG2_MIB_MIBP04 ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE
     1       1       0.173239 SELECT FILE_NM,SRC_STM_ID,MSR_PRD_TP,PRV_EXTR_TMST,MSR_PRD,MSR_PRD_DT FROM DSSSTG.V_SG_CTL1 V_SG_CTL1 WHERE SRC_STM_ID = ?AND FILE_NM='DSCLQ17.DAT'
     4       1       0.011126 SELECT partitionmap FROM sysibm.systables, sysibm.syspartitionmaps WHERE sysibm.systables.pmap_id = sysibm.syspartitionmaps.pmap_id AND sysibm.systables.name='SG_SRC_X_AR' AND sysibm.systables.creator='DSSSTG  ' FOR READ ONLY

These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 09-04-2013
Awesome
# 5  
Old 09-05-2013
Thanks for the sharing..your input is very much great and awesome!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Transpose columns to row

Gents Using the attached file and using this code. awk '{print substr($0,4,2)}' input.txt | sort -k1n | awk '{a++}END{for(i in a) print i,a}' | sort -k1 > output i got the this output. 00 739 01 807 02 840 03 735 04 782 05 850 06 754 07 295 08 388 09 670 10 669 11 762 (8 Replies)
Discussion started by: jiam912
8 Replies

2. Shell Programming and Scripting

How to display the header of a matched row in a file?

Hi, So I am trying to print the first row(header) first column alongwith the matched value. But I am not sure how do I print the same, by matching a pattern located in the file eg File contents Name Place Jim NY Jill NJ Cathy CA Sam TX Daniel FL And what I want is... (2 Replies)
Discussion started by: sidnow
2 Replies

3. UNIX for Dummies Questions & Answers

Select 2 columns and transpose row by row

Hi, I have a tab-delimited file as follows: 1 1 2 2 3 3 4 4 a a b b c c d d 5 5 6 6 7 7 8 8 e e f f g g h h 9 9 10 10 11 11 12 12 i i j j k k l l 13 13 14 14 15 15 16 16 m m n n o o p p The output I need is: 1 1 a a 5 5 e e 9 9 i i 13... (5 Replies)
Discussion started by: mvaishnav
5 Replies

4. Shell Programming and Scripting

Replacing a pattern in different cases in different columns with a single pattern

Hi All I am having pipe seperated inputs like Adam|PeteR|Josh|PEter Nick|Rave|Simon|Paul Steve|smith|PETER|Josh Andrew|Daniel|StAlin|peter Rick|PETer|ADam|RAVE i want to repleace all the occurrence of peter (in any case pattern PeteR,PEter,PETER,peter,PETer) with Peter so that output... (5 Replies)
Discussion started by: sudeep.id
5 Replies

5. UNIX for Dummies Questions & Answers

help [[row and columns]]

i ask to do ,,program that convert the last row to be the first row ,,,and after that exchange the the columns ex,, 1 2 3 4 5 6 7 8 9 to be 7 8 9 4 5 6 1 2 3 and then to be 9 8 7 6 5 4 3 2 1 (0 Replies)
Discussion started by: khaled1989kh
0 Replies

6. Shell Programming and Scripting

help [[row and columns]]

i ask to do ,,program that convert the last row to be the first row ,,,and after that exchange the the columns ex,, 1 2 3 4 5 6 7 8 9 to be 7 8 9 4 5 6 1 2 3 and then to be 9 8 7 6 5 4 3 2 1 give mee the code .... (0 Replies)
Discussion started by: khaled1989kh
0 Replies

7. Shell Programming and Scripting

how to display column value in a row with space as delimeter

Hi I need to write a small script to kill the process id of particular job in one shot , Example > ps PID TTY TIME COMMAND 16280 pts/70 0:00 sh 16278 pts/70 0:00 rlogind 16197 pts/70 0:00 ps 1234 pts/70 0:00 runflow 2341 pts/70 0:00 runflow 12673 pts/70 ... (6 Replies)
Discussion started by: mani_isha
6 Replies

8. Shell Programming and Scripting

Want to grep for a pattern and display the content above that pattern

Hi, When we have a failure, sometimes we just step restart the job from the next step. Later when we open the log for analysis of the failure, it is becoming difficult to go to the failure part. For eg., if it is a 1000 line log, the failure may be at 500th line. so wat i want to do is, grep... (6 Replies)
Discussion started by: ajayakunuri
6 Replies

9. UNIX for Dummies Questions & Answers

Row to Columns

Hi, I have a file like this. 1,1,1,0,0,0 1,1,2,1,0,0 1,1,3,0,0,0 1,1,4,0,0,0 ........... ........... 1,1,24,0,0,0 1,1,25,0,0,0 1,1,26,1,0,0 1,1,27,0,0,0 1,2,1,0,0,0 1,2,2,0,0,0 1,2,3,0,0,0 1,2,4,0,0,0 1,2,5,1,0,0 1,2,6,1,0,0 (4 Replies)
Discussion started by: vskr72
4 Replies

10. UNIX for Dummies Questions & Answers

display column in a row

how can i display every column in a row individually........ i vauguely remmeber something like echo $1 $2 etc.......but i dont remmeber properly......so is there anything like that? i tried searching but wasnt able to find...... thanks and regards vivek.s (2 Replies)
Discussion started by: vivekshankar
2 Replies
Login or Register to Ask a Question