conditional split


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting conditional split
# 1  
Old 03-07-2006
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
# 2  
Old 03-08-2006
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.
# 3  
Old 03-10-2006
Hi,
There would only four fields for each record split.
# 4  
Old 03-10-2006
Code:
ruby -ne 'scan(/([a-z]+ ?(\d)\d*,(?:[a-z]+ ?\2\d*,?)*)/i ).each{|x|
  puts x.first.chomp(",") }' myfile

# 5  
Old 03-10-2006
Hi Futurelet,
could you tell me what is this command 'RUBY' ? Is this pearl ?????
Iam looking to acheive this in shell scripting.
# 6  
Old 03-11-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Conditional Split

Greetings, I need help in splitting the files in an efficient way while accommodating the below requirements . I am on AIX. Split condition Split the file based on the record type and the position of the data pattern that appears on the on the record type. Both record type and and the... (9 Replies)
Discussion started by: techedipro
9 Replies

2. UNIX for Beginners Questions & Answers

Split and Rename Split Files

Hello, I need to split a file by number of records and rename each split file with actual filename pre-pended with 3 digit split number. What I have tried is the below command with 2 digit numeric value split -l 3 -d abc.txt F (# Will Produce split Files as F00 F01 F02) How to produce... (19 Replies)
Discussion started by: techedipro
19 Replies

3. Shell Programming and Scripting

awk to format file with conditional split

In the awk below I am splitting $7 on the : (colon) then - (hyphen) as array a. The word chr is printed at the start of every $1 line. Next, $4 is split on the > (greater then) as array b. I am not sure how to account for the two other possibilities in $4 so the correct output is printed. Every... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

5. Shell Programming and Scripting

Conditional variables

if {-a file} if {-s file} what it will do if {-z string} what it will do please help on this (1 Reply)
Discussion started by: thelakbe
1 Replies

6. Shell Programming and Scripting

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (12 Replies)
Discussion started by: abhinavsinha
12 Replies

7. UNIX for Dummies Questions & Answers

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (1 Reply)
Discussion started by: abhinavsinha
1 Replies

8. UNIX for Dummies Questions & Answers

conditional

conditional is not wworking can any one figure out what goes wrong xx1=`$ORACLE_HOME/bin/sqlplus -s apps/ostgapps1 2>/dev/null << EOF WHENEVER SQLERROR EXIT 1 set head off feedback off ; WHENEVER SQLERROR EXIT SQL.SQLCODE; select count(*) from CMS_INVOICE_ALL... (2 Replies)
Discussion started by: u263066
2 Replies

9. Shell Programming and Scripting

Conditional FTP

Hi Have a Nice Day i m stuck with this quite easy task assume that i m in certain directory and i open a ftp connection from there to some other directory and in second directory there are like so many files from july 02 till today what would be the simple unix command to ftp all files from this... (2 Replies)
Discussion started by: Dastard
2 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question