The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #11 (permalink)  
Old 10-13-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
Quote:
Originally Posted by pkumar3 View Post
Hi I have a similar problem.
source file is of format
1,a,b,c,d,e,f,g,h
2
3,f,g,h,i,l
4
5,m,n

And the expected Output file
1,a,b,c
1,d,e,f
1,g,h
3,f,g,h
3,i,l
5,m,n

I used this method
awk -F, '$1!=p1{printf "%s,%s,%s,%s\n",$1,$2,$3,$4} $2!=p2{printf "%s,%s,%s,%s\n",$1,$5,$6,$7,$8; p1=$1;p2=$2}'

the no of coloumns in source file is of varriable length and can grow upto 3000.

Code:
perl -F, -nae'if($#F){s/((?:(?:.*?),){3}[^,]*)/$1\n$F[0]/g;print}' filename