Alter awk script to reformat output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Alter awk script to reformat output
# 1  
Old 02-16-2016
Alter awk script to reformat output

Data:

Code:
0,mfrh_green_screen,1455432969,37540,/prod/test/system/sys/unikixmain.log,3.0M,mfrh_green_screen,3120660,0,36964--37540
0,mfrh_green_screen,1455433269,38100,/prod/test/system/sys/unikixmain.log,3.1M,mfrh_green_screen,3164223,0,37540--38100
0,mfrh_green_screen,1455433569,38651,/prod/test/system/sys/unikixmain.log,3.1M,mfrh_green_screen,3207015,0,38100--38651 0,mfrh_green_screen,1455433869,39561,/prod/test/system/sys/unikixmain.log,3.2M,mfrh_green_screen,3278621,0,38651--39561

When I run the below code on the above data:

Code:
awk -v SEARCHPATT="Feb 13" -F, '/,1455/,0 {A=strftime("%a %b %d %T %Y,%s",$3);{Q=1};if( (Q) && (NF == 13) && (A ~ SEARCHPATT)) {prev=$3; print; getline; print $0",""lgxtime"","($3-prev","$3"_"prev)} else if ( (Q) && (NF == 10) && (A ~ SEARCHPATT) ) {prev=$3; print; getline; print $0",""lgxtime"","($3-prev"-"$3"_"prev""--""0")}  }' datafile

I get an output similar to this:

Code:
0,mfrh_green_screen,1455432969,37540,/prod/test/system/sys/unikixmain.log,3.0M,mfrh_green_screen,3120660,0,36964--37540
0,mfrh_green_screen,1455433269,38100,/prod/test/system/sys/unikixmain.log,3.1M,mfrh_green_screen,3164223,0,37540--38100,lgxtime,300-1455433269_1455432969--0
0,mfrh_green_screen,1455433569,38651,/prod/test/system/sys/unikixmain.log,3.1M,mfrh_green_screen,3207015,0,38100--38651
0,mfrh_green_screen,1455433869,39561,/prod/test/system/sys/unikixmain.log,3.2M,mfrh_green_screen,3278621,0,38651--39561,lgxtime,300-1455433869_1455433569—0

Which is fine. But I need the output to look this way:

Code:
0,mfrh_green_screen,1455432969,37540,/prod/test/system/sys/unikixmain.log,3.0M,mfrh_green_screen,3120660,0,36964--37540,lgxtime,300-1455433269_1455432969--0
0,mfrh_green_screen,1455433269,38100,/prod/test/system/sys/unikixmain.log,3.1M,mfrh_green_screen,3164223,0,37540--38100,lgxtime,300-1455433569_1455432969--0
0,mfrh_green_screen,1455433569,38651,/prod/test/system/sys/unikixmain.log,3.1M,mfrh_green_screen,3207015,0,38100--38651,lgxtime,300-1455433869_1455433569--0
0,mfrh_green_screen,1455433869,39561,/prod/test/system/sys/unikixmain.log,3.2M,mfrh_green_screen,3278621,0,38651--39561,lgxtime,and so on……..

Basically, what’s going on here is, I'm reading the data file, getting the value that’s in the 3rd field of the current line, get next line, get the value that’s in the 3rd field of that line,
subtract current line's value from value of the next line, then add it to the end of the current line when it is printed out. Prefix the output with ",lgxtime,".
# 2  
Old 02-17-2016
Why compare the current line with the next line?
It is easier to compare the current line with the previous line
Code:
awk 'BEGIN {FS=OFS=","; prev="0"} {print $0,"lgxtime",$3-prev"="$3"-"prev; prev=$3}' datafile

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Reformat awk output

I need to rearrange the output but i am unable to arrange it to match the format. In the output i need NAME=\"To in the column . Bash: #!/bin/bash cd /cygdrive/c/output/a cat *.txt > output.txt i=/cygdrive/c/output/a/output.csv #echo "NE_Name, Source, Destination, OSPF_AREA_ID"... (4 Replies)
Discussion started by: adgjmpt
4 Replies

3. Shell Programming and Scripting

Using awk to reformat file output

Hi there. I need to reformat a large file. Here is a sample of the file. NETIK0102_UCS_Boot_a,NETIK0102_UCS_Boot_b 5200 2438 70G 5200 2439 70G NETIK0102_UCS_HBA0_a,NETIK0102_UCS_HBA1_b,NETIK0102_UCS_HBA2_a,NETIK0102_UCS_HBA3_b 2673 19D7 55G 2673 19C0 30G 2673 19F5 120G... (5 Replies)
Discussion started by: kieranfoley
5 Replies

4. Shell Programming and Scripting

Use search pattern to reformat the output

I have below file listing ] ls -1 *.txt MISTradesReport_141105_d130240_VOLCKER_EMEA_LOANIQ_FEED_2013-12-24.txt MISTradesReport_141106_d130240_VOLCKER_NA_LOANIQ_FEED_2013-12-24.txt MISTradesReport_141107_d130240_VOLCKER_EMEA_CDS_CRDI_FEED_2013-12-24.txt... (4 Replies)
Discussion started by: krg.sati
4 Replies

5. Shell Programming and Scripting

awk to reformat text

I have this input and want output like below, how can I achieve that through awk: Input: CAT1 FRY-01 CAT1 FRY-04 CAT1 DRY-03 CAT1 FRY-02 CAT1 DRY-04 CAT2 FRY-03 CAT2 FRY-02 CAT2 DRY-01 FAT3 DRY-12 FAT3 FRY-06 Output: category CAT1 item FRY-01 (7 Replies)
Discussion started by: aydj
7 Replies

6. Shell Programming and Scripting

need awk or sed help to reformat output

We have the following output: server1_J00_data_20120711122243 server1_J00_igs_20120711122243 server1_J00_j2ee_20120711122243 server1_J00_sec_20120711122243 server1_J00_data_20120711131819 server1_J00_igs_20120711131819 server1_J00_j2ee_20120711131819 server2_J00_data_20120711122245... (10 Replies)
Discussion started by: ux4me
10 Replies

7. UNIX for Advanced & Expert Users

Script to reformat output

Hi colleagues, I have the followind script. db2 -x "select substr(TBSPACE,1,20) TABLESPACE from syscat.tables where tabschema = 'SCHEMA' and tabname like '%XXXX' group by TBSPACE order by TBSPACE" | awk '{print $1}' | while read tablespace do db2 "list tablespaces show detail" |grep -p -w... (5 Replies)
Discussion started by: systemoper
5 Replies

8. UNIX for Advanced & Expert Users

reformat ps output

I often use "ps -ef" command to list all running processes. Now i want to customize the output to show only 2 parts: CMD and UID as below: /bin/bash /usr/bin/run-parts /etc/cron.weekly root /usr/sbin/httpd apache /usr/sbin/httpd apache /usr/sbin/httpd apache I use ps -ef | awk '{print $8"... (3 Replies)
Discussion started by: fongthai
3 Replies

9. Shell Programming and Scripting

Alter Table Shell Script

I want to add some columns to a existing tables through a shell script. Please help. (2 Replies)
Discussion started by: ankitgupta
2 Replies

10. Shell Programming and Scripting

To alter perl /bin/date output by - 5 hours

Hello! I'm using a perl script which calls the time and date from a remote server using the line /bin/date - What is needed in this line to reduce the output time 5 hours? Thanks (2 Replies)
Discussion started by: Texan
2 Replies
Login or Register to Ask a Question