Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-12-2012
Registered User
 
Join Date: Jul 2006
Posts: 40
Thanks: 24
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old 07-12-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,370
Thanks: 87
Thanked 476 Times in 456 Posts

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...
The Following User Says Thank You to elixir_sinari For This Useful Post:
ux4me (07-12-2012)
Sponsored Links
    #3  
Old 07-12-2012
Skrynesaver's Avatar
Grumpy old geek
 
Join Date: Mar 2011
Location: Éire
Posts: 660
Thanks: 19
Thanked 147 Times in 144 Posts
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
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
 
Join Date: Feb 2006
Location: Almería, Spain
Posts: 695
Thanks: 14
Thanked 84 Times in 80 Posts
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}'

The Following User Says Thank You to Klashxx For This Useful Post:
ux4me (07-12-2012)
Sponsored Links
    #5  
Old 07-12-2012
Registered User
 
Join Date: Jul 2012
Posts: 194
Thanks: 0
Thanked 38 Times in 38 Posts

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 03:53 PM.. Reason: Please use code tags for data and code samples, thank you
The Following User Says Thank You to raj_saini20 For This Useful Post:
ux4me (07-12-2012)
Sponsored Links
    #6  
Old 07-12-2012
Registered User
 
Join Date: Jul 2006
Posts: 40
Thanks: 24
Thanked 0 Times in 0 Posts
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!
Sponsored Links
    #7  
Old 07-12-2012
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
 
Join Date: Oct 2003
Location: 54.23, -4.53
Posts: 1,792
Thanks: 1
Thanked 101 Times in 91 Posts
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

The Following User Says Thank You to Ygor For This Useful Post:
ux4me (07-12-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to reformat output systemoper UNIX for Advanced & Expert Users 5 06-26-2012 07:48 AM
reformat HHMMSSmmm into HH:MM:SS.mmm migurus Shell Programming and Scripting 2 06-08-2012 02:57 PM
reformat ps output fongthai UNIX for Advanced & Expert Users 3 09-23-2008 05:22 AM
help reformat data with awk climbak Shell Programming and Scripting 4 05-30-2008 02:17 PM
reformat the output from "diff" command CamTu Shell Programming and Scripting 5 03-01-2005 09:54 AM



All times are GMT -4. The time now is 09:15 AM.