Another tricky sed or awk question

This post is in reference to http://www.unix.com/shell-programming-scripting/137977-tricky-sed-awk-question-post302428154.html\#post302428154

I am trying to go the opposite direction now:

I have the following file:

a,b,C,f,g
a,b,D,f,g
a,b,E,f,g
h,i,J,k,l
m,n,O,t,u
m,n,P,t,u
m,n,Q,t,u
m,n,R,t,u
m,n,S,t,u
v,w,X,z,a
v,w,Y,z,a

I want to use sed or awk (perl also welcome), to "condense" these lines into this format:

a,b,C-D-E,f,g
h,i,J,k,l
m,n,O-P-Q-R-S,t,u
v,w,X-Y,z,a

Who is up for it? You would again save me tons of time!

Thanks a lot in advance!

nawk '
   BEGIN {
     FS=OFS=","
     SEP="-"
     stub="<stub>"
   }
   {
     idx=$1 OFS $2 OFS stub OFS $4 OFS $5
     a[idx]=(idx in a)?a[idx] SEP $3:$3
   }
   END {
     for(i in a) {
       sub(stub, a, i)
       print i
     }
   }' myFile

1 Like

perfect, thanks!

$ perl -F, -ane '{
                  if ( @A[0] eq "" ) { push @A, @F ; }
                  elsif ( $A[0] ne "" && $A[0] eq $F[0] ) { $A[2]="$A[2]-$F[2]"; }
                  elsif ( $A[0] ne "" && $A[0] ne $F[0] ) { print "$A[0],$A[1],$A[2],$A[3],$A[4]" ; @A=(); push @A, @F; }
                }
                 END
                {
                  print "$A[0],$A[1],$A[2],$A[3],$A[4]"; 
                }' infile