Tranform Log to Row Base Text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tranform Log to Row Base Text
# 1  
Old 04-18-2017
Tranform Log to Row Base Text

Greeting Everyone,
Anyone has tested to transform a log file seemingly as following
Code:
  Text     : SELECT DISTINCT Block_Code FROM   DSSBDW.CC_PL_TRX
  -------------------------------------------
  Start Time: 04/18/2017 20:08:20.840551
  Stop Time:  04/18/2017 20:08:20.846921
  Elapsed Execution Time:  0.006370 seconds
  Number of Agents created: 1
  User CPU: 0.001788 seconds
  System CPU: 0.000168 seconds

  Text     : Select * from DSSBDW.RI  where UNQ_ID_SRC_STM = '2114999''
  -------------------------------------------
  Start Time: 04/18/2017 14:47:50.065624
  Stop Time:  04/18/2017 14:47:50.065662
  Elapsed Execution Time:  0.000038 seconds
  Number of Agents created: 1
  User CPU: 0.000020 seconds
  System CPU: 0.000001 seconds

Expected Output as following

Code:
Text                                   ElapsedTime  UserCPU   SystemCPU
----                                    ----------       -------   ---------
SELECT DISTINCT Block_Code FROM   DSSBDW.CC_PL_TRX     0.006370    0.001788   0.000168
Select * from DSSBDW.RI  where UNQ_ID_SRC_STM = '2114999'  0.000038    0.000020   0.000001

Thanks Everyone.
# 2  
Old 04-19-2017
Without knowing the maximum width of the text in your real data, without knowing what operating system or shell you're using, without knowing the name of the log file you want to process, assuming that the text in your input file should be copied verbatim (not dropping any characters as was done in your sample output), nor if you really want seemingly random spacing in the output you produce, you might trying using something like the following as a 1st guess at something that might work:
Code:
awk '
BEGIN {	fmt = "%-60s %-12s %-10s %s\n"
	printf(fmt, "Text", "ElapsedTime", "UserCPU", "SystemCPU")
	printf(fmt, "----", "-----------", "-------", "---------")
}
$1 == "Text" {
	text = substr($0, index($0, ":") + 2)
	next
}
$1 == "Elapsed" || $1 == "User"  || $1 == "System" {
	times[$1] = $(NF - 1)
}
$1 == "System" {
	printf(fmt, text, times["Elapsed"], times["User"], times["System"])
}' log

which, if the sample input you provided in post #1 in this thread is in a text file named log, produces the following output:
Code:
Text                                                         ElapsedTime  UserCPU    SystemCPU
----                                                         -----------  -------    ---------
SELECT DISTINCT Block_Code FROM   DSSBDW.CC_PL_TRX           0.006370     0.001788   0.000168
Select * from DSSBDW.RI  where UNQ_ID_SRC_STM = '2114999''   0.000038     0.000020   0.000001

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
# 3  
Old 04-19-2017
Try
Code:
awk -F":"       '
BEGIN           {HD = "Text     ElapsedExecutionTime    UserCPU SystemCPU"
                 for (MX=n=split (HD, HDArr, OFS); n>0; n--) SRCH[HDArr[n]]
                 print HD
                 gsub (/[^\t]/, "-", HD)
                 print HD
                }

!NF             {for (i=1; i<=MX; i++) printf "%s%s", RES[HDArr[i]], (i == MX)?ORS:OFS
                 delete RES
                }

                {gsub (/^[ "]*| *$/, "", $1)
                }

$1 in SRCH      {RES[$1] = $0
                 sub ($1, "", RES[$1])
                 sub (/ *seconds *$/, "", RES[$1])
                 sub (/^[ \t]*/, "", RES[$1])
                }

END             {for (i=1; i<=MX; i++) printf "%s%s", RES[HDArr[i]], (i == MX)?ORS:OFS
                }

' OFS="\t" file
Text    ElapsedExecutionTime    UserCPU    SystemCPU
----    --------------------    -------    ---------
SELECT DISTINCT Block_Code FROM   DSSBDW.CC_PL_TRX    0.006370    0.001788    0.000168
Select * from DSSBDW.RI  where UNQ_ID_SRC_STM = '2114999''    0.000038    0.000020    0.000001

# 4  
Old 04-19-2017
Thanks Don and RudiC for your contribution, my environment is AIX server Power 8. The above log was generated by database event monitor file and I would like to tabular it into row base report for easy traceability purpose.

I will try your awesome script. Thanks again guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Column to Row base on first and second Column

Input :- Column to Row Time CT 1 2 3 17:45 X 10 15 20 18:00 X 15 20 30 18:15 X 10 10 10 18:30 X 5 5 5 17:45 Y 10 15 20 18:00 Y 15 20 30 18:15 Y 10 10 10 18:30 Y 5 5 5 output:- Time P X Y 17:45 1 10 10 18:00 1 15 15 18:15 1 10 10 (2 Replies)
Discussion started by: pareshkp
2 Replies

2. Shell Programming and Scripting

At text to field 1 of header row using awk

I am just trying to insert the word "Index" using awk. The below is close but seems to add the word at the end and I can not get the syntax correct to add from the beginning. Thank you :). awk -F'\t' -v OFS='\t' '{ $-1=$-1 OFS "Index"}$1=$1' file current output Chr Start End ... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

How to row bind two text files?

Hi, I have two text files with approximately 6000 rows each. I wish the bind these two files into a single column in a new text file like this: File 1: EQTN AFAF SPACA8 equatorin ... File 2: DA3 DA5 FAM38B2 HsT748 ... (2 Replies)
Discussion started by: bronzyroo
2 Replies

4. Shell Programming and Scripting

Select row from file and text

Hi all! I would like to solve a problem but I have no clue of how do it!I will be grateful if someone could help me! Briefly I have a big file like this: >ENSMUSG00000000204 | ENSMUST00000159637 GGCGAGGCTTACGCCATTTTACCTCAGCGAGCATTCATAAAGCTGCGAGCATTCATACAG >ENSMUSG00000000204 |... (3 Replies)
Discussion started by: giuliangiuseppe
3 Replies

5. Shell Programming and Scripting

Getting the non-homogenous letter row from a text file

I do have a large tab delimited file with the following format CCCCCGCCCCCCCCCCcCCCCCCCCCCCCCCCC 23 65 3 4 AAAAAAAAAAAAAAAAaAAAAAAAAAAAAAAAA 24 6 89 90 TGTTTTTTTTTTTTGGtTTTTTTTTTTTTTTTT 2 4 8 90 TTTT-TTTTTTTTTTTtTTTTTTTTTTTTTTTT 1 34 89 50 GGGGGGGGGGGGGGGGTGGGGGGGGGGGGGGGG 87 6 78 66... (8 Replies)
Discussion started by: Lucky Ali
8 Replies

6. UNIX for Dummies Questions & Answers

Adding a column to a text file with row numbers

Hi, I would like to add a new column containing the row numbers to a text file. How do I go about doing that? Thanks! Example input: A X B Y C D Output: A X 1 B Y 2 C D 3 (5 Replies)
Discussion started by: evelibertine
5 Replies

7. Shell Programming and Scripting

Change column to row base on column 2

Hi Guys, Input.txt L194 A -118.2 L194 B -115.1 L194 C -118.7 L196 A 0 L196 C 0 L197 A -111.2 L197 B -118.9 L197 C -119.9 L199 A -120.4 L199 B -119.9 ... (2 Replies)
Discussion started by: asavaliya
2 Replies

8. Shell Programming and Scripting

Replacing the text in a row based on certain condition

Hi All, I felt tough to frame my question. Any way find my below input. (.CSV file) SNo, City 1, Chennai 2, None 3, Delhi 4,None Note that I have many rows ans also other columns beside my City column. What I need is the below output. SNo, City 1, Chennai 2, Chennai_new 3, Delhi... (2 Replies)
Discussion started by: ks_reddy
2 Replies

9. Shell Programming and Scripting

how can I bcp out a table into a text file including the header row in the text file

Hi All, I need to BCP out a table into a text file along with the table headers. Normal BCP out command only bulk copies the data, and not the headers. I am using the following command: bcp database1..table1 out file1.dat -c -t\| -b1000 -A8192 -Uuser -Ppassword -efile.dat.err Regards,... (0 Replies)
Discussion started by: shilpa_acc
0 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question