need awk or sed help to reformat output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need awk or sed help to reformat output
# 1  
Old 07-12-2012
need awk or sed help to reformat output

We have the following output:

Code:
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
server2_J00_igs_20120711122245
server2_J00_j2ee_20120711122245
server2_J00_sec_20120711122245
server2_J00_data_20120711131821
server2_J00_igs_20120711131821
server2_J00_j2ee_20120711131821
server2_J00_sec_20120711131821

Instead of using array programming, is there a simpler solution using awk or sed to reformat the above output to this:

Code:
20120711122243
          server1_J00_data
          server1_J00_igs
          server1_J00_j2ee
          server1_J00_sec

20120711122245
          server2_J00_data
          server2_J00_igs
          server2_J00_j2ee
          server2_J00_sec

20120711131819
          server1_J00_data
          server1_J00_igs
          server1_J00_j2ee

20120711131821
          server2_J00_data
          server2_J00_igs
          server2_J00_j2ee
          server2_J00_sec

Please advise and thanking you in advance.
# 2  
Old 07-12-2012
Code:
awk -F_ '{
a[$NF]=a[$NF]?a[$NF]"\n\t"substr($0,1,(index($0,$NF)-2)):$NF"\n\t"substr($0,1,(index($0,$NF)-2))
}END{for(i in a) printf("%s\n\n",a[i])}' inputfile

EDIT: Hope it's not a huge file...Smilie
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 07-12-2012
A Perl solution:
Code:
-bash-3.00$ echo '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
server2_J00_igs_20120711122245
server2_J00_j2ee_20120711122245
server2_J00_sec_20120711122245
server2_J00_data_20120711131821
server2_J00_igs_20120711131821
server2_J00_j2ee_20120711131821
server2_J00_sec_20120711131821' | perl -e ' while(<STDIN>){push @{$servers{$2}}, $1 if /^(.+)_(\d+)$/;} for $date (keys %servers){print "$date\n";for $server (@{$servers{$date}}){print "\t$server\n"}}'
20120711131821
        server2_J00_data
        server2_J00_igs
        server2_J00_j2ee
        server2_J00_sec
20120711122245
        server2_J00_data
        server2_J00_igs
        server2_J00_j2ee
        server2_J00_sec
20120711122243
        server1_J00_data
        server1_J00_igs
        server1_J00_j2ee
        server1_J00_sec
20120711131819
        server1_J00_data
        server1_J00_igs
        server1_J00_j2ee
-bash-3.00$

# 4  
Old 07-12-2012
If you have a huge file i would use this:
Code:
 sort -t_ -T /path/for/temporary/scratch/files -n -k 4.4 bigfile|awk -F\_ 'a!=$NF{print $NF;a=$NF}{print "\t"$1""FS""$2""FS""$3}'

This User Gave Thanks to Klashxx For This Post:
# 5  
Old 07-12-2012
Code:
awk -F"_" 'BEGIN{x=0}{if($4==x){print "\t"$1"_"$2"_"$3}else{print $4;print "\t"$1"_"$2"_"$3};x=$4}' inputfile


Last edited by Franklin52; 07-12-2012 at 04:53 PM.. Reason: Please use code tags for data and code samples, thank you
This User Gave Thanks to raj_saini20 For This Post:
# 6  
Old 07-12-2012
Thanks to everyone for the quick aid.. got it working using this on the output (small file):

Code:
sort -t '_' +3 | awk -F"_" 'BEGIN {x=0}{if($4==x){print "\t"$1"_"$2"_"$3}else{print "\n"$4;print "\t"$1"_"$2"_"$3};x=$4}'

---------- Post updated at 07:07 AM ---------- Previous update was at 06:36 AM ----------

Made some modifications and got this output:
Code:
Time: 20120620133029   Host: ddaap020   Instance: J00
        data
        igs
        j2ee
        sec

Time: 20120620133031   Host: ddaap021   Instance: J00
        data
        igs
        j2ee
        sec

Time: 20120630011207   Host: ddaap020   Instance: J00
        data
        igs
        j2ee
        sec

Time: 20120630011209   Host: ddaap021   Instance: J00
        data
        igs
        j2ee
        sec

Time: 20120710005004   Host: ddaap020   Instance: J00
        data
        igs
        j2ee
        sec

Time: 20120710005006   Host: ddaap021   Instance: J00
        data
        igs
        j2ee
        sec

Time: 20120711122243   Host: ddaap020   Instance: J00
        data
        igs
        j2ee
        sec

Time: 20120711122245   Host: ddaap021   Instance: J00
        data
        igs
        j2ee
        sec

Time: 20120711131819   Host: ddaap020   Instance: J00
        data
        igs
        j2ee

Time: 20120711131821   Host: ddaap021   Instance: J00
        data
        igs
        j2ee
        sec

Sorry to bother you folks again but now I want the output to look like this:
Code:
Time: 20120620133029   Host: server1   Instance: J00   Dirs: data,igs,j2ee,sec

Time: 20120620133031   Host: server2   Instance: J00   Dirs: data,igs,j2ee,sec

Time: 20120630011207   Host: server1   Instance: J00   Dirs: data,igs,j2ee,sec

Time: 20120630011209   Host: server2   Instance: J00   Dirs: data,igs,j2ee,sec

Time: 20120710005004   Host: server1   Instance: J00   Dirs: data,igs,j2ee,sec

Time: 20120710005006   Host: server2   Instance: J00   Dirs: data,igs,j2ee,sec

Time: 20120711122243   Host: server1   Instance: J00   Dirs: data,igs,j2ee,sec

Time: 20120711122245   Host: server2   Instance: J00   Dirs: data,igs,j2ee,sec

Time: 20120711131819   Host: server1   Instance: J00   Dirs: data,igs,j2ee

Time: 20120711131821   Host: server2   Instance: J00   Dirs: data,igs,j2ee,sec

Thank you all so much!

---------- Post updated at 07:25 AM ---------- Previous update was at 07:07 AM ----------

got it working by passing another awk. The final statement is:
Code:
sort -t '_' +3 | awk -F"_" 'BEGIN {x=0}{if($4==x){print "\t"$3}else{print "\tTime: "$4"   Host: "$1"   Instance: "$2"   Dirs: ";print "\t"$3};x=$4}' | awk '/Time:/{if (x)print x;x="";}{x=(!x)?$0:x""$0;}END{print x;}'

would be nice if I could do it within 1 awk statement, but this would suffice, I guess...

Thanks to all again for the awk-some statement!
# 7  
Old 07-12-2012
All in one...
Code:
awk 'BEGIN {
        FS = "_"
        fmt = "%sTime: %s  Host: %s  Instance: %s  Dirs: "
     }
     {
        if ($4 != prev) {
            printf fmt, (dirs?dirs ORS:""), $4, $1, $2
            prev = $4
            dirs = $3
         } else {
            dirs = dirs "," $3
         }
     }
     END {print dirs}' file1 > file2

This User Gave Thanks to Ygor 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

Alter awk script to reformat output

Data: 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... (1 Reply)
Discussion started by: SkySmart
1 Replies

4. 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

5. 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

6. Shell Programming and Scripting

awk reformat file

Hello: When I tried a perl-oneliner to re-format fasta file. infile.fasta >YAL069W-1.334 Putative promoter CCACACCACACCCACACACC ACACCACACCCACACACACA ACAGCCCTAATCTAACCC >YAL068C-7235.2170 Putative ABC sequence TACGAGAATAATTT ACGTAAATGAAGTT TATATATAAA >gi|31044174|gb|AY143560.1|... (15 Replies)
Discussion started by: yifangt
15 Replies

7. 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

8. 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

9. 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

10. Shell Programming and Scripting

reformat date, awk and sed

The command below is getting me the output I need. awk -F"," ' { if ($6 = 475) print "@@"$3 " " "0000" $10 "0" $1 "00000000" $8}' ${DIR1}${TMPFILE1} | sed -e 's/@@1//g' > ${DIR2}${TPRFILE} Output: 900018732 00004961160200805160000000073719 Now I need to incorporate... (5 Replies)
Discussion started by: mondrar
5 Replies
Login or Register to Ask a Question