The UNIX and Linux Forums  

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


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Conditional FTP Dastard Shell Programming and Scripting 2 06-21-2008 04:38 AM
conditional email ricky007 Shell Programming and Scripting 3 03-05-2008 10:38 PM
AWK - conditional cause Rafael.Buria Shell Programming and Scripting 2 01-28-2008 10:24 AM
Split a file with no pattern -- Split, Csplit, Awk madhunk UNIX for Dummies Questions & Answers 10 12-17-2007 09:57 AM
Conditional Statements cstovall Shell Programming and Scripting 1 05-15-2005 02:58 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-07-2006
Registered User
 

Join Date: Jul 2005
Posts: 67
conditional split

Hi,

Can someone let me know how I can split a record when it contains a vairable length of fields.

Code:
Line1

field1,field101,field102,field 103,field104

Line 2

field1,field101,field102,field 103,field104,field201,field202,field 203,field204

Line 3
field1,field101,field102,field 103,field104,field201,field202,field 203,field204,field301,field 302,field303,field304

In the above scenario,I wanna blow up individual record for the number of times the field101..field104 or field201...field204 or field 301..304 exists.

So for the above example the expected output would be,

Line 1 would remain as such.

Line 2 would be splitted into 2 where field101,..field104(record1) and field201..field204(record 2)

Line 3 would be splitted into 3 where field101,..field104(record1) ,field201..field204(record 2) and field301..field304(record 3)

Appreciate your inputs.

Thx
Reply With Quote
Forum Sponsor
  #2  
Old 03-07-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
For each record, would there be only 4 fields ? Or does that vary. If it varies, then you need to show us what the fields look like.
Reply With Quote
  #3  
Old 03-10-2006
Registered User
 

Join Date: Jul 2005
Posts: 67
Hi,
There would only four fields for each record split.
Reply With Quote
  #4  
Old 03-10-2006
Registered User
 

Join Date: Jul 2005
Posts: 137
Code:
ruby -ne 'scan(/([a-z]+ ?(\d)\d*,(?:[a-z]+ ?\2\d*,?)*)/i ).each{|x|
  puts x.first.chomp(",") }' myfile
Reply With Quote
  #5  
Old 03-10-2006
Registered User
 

Join Date: Jul 2005
Posts: 67
Hi Futurelet,
could you tell me what is this command 'RUBY' ? Is this pearl ?????
Iam looking to acheive this in shell scripting.
Reply With Quote
  #6  
Old 03-11-2006
Registered User
 

Join Date: Dec 2005
Location: India
Posts: 218
Hi Braindrain,

this will work

Quote:
$ cat f1.txt|awk -F ',' '{
> for(i=1;i<NF;i+=4)
> print $i, $(i+1), $(i+2), $(i+3)
> }'
where f1.txt is ur input file,

Gaurav
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0