Split file based on multi valued attributes


 
Thread Tools Search this Thread
Operating Systems Solaris Split file based on multi valued attributes
# 1  
Old 01-31-2011
Split file based on multi valued attributes

Hi,
I am new to shell scripting.
I have a file which has multi valued attributes. I wanted to split it so that there will be no muliti valued attributes.
Example file:
Code:
"attr1","amv1;amv2;3","bmv1;bmv2","abc","abc1;abc2;abc3"

Plz note this is CSV file and ; is the delimiter for multi valued attributes.

The out put shoud be:
Code:
"attr1","amv","bmv1","abc","abc1"
"attr1","amv1","bmv1","abc","abc2"
"attr1","amv1","bmv1","abc","abc3"

"attr1","amv1","bmv2","abc","abc1"
"attr1","amv1","bmv2","abc","abc2"
"attr1","amv1","bmv2","abc","abc3" etc

Please help.

Last edited by Scott; 01-31-2011 at 11:18 PM.. Reason: Please use code tags
# 2  
Old 01-31-2011
are you sure there is always 5 columns in your file?if not,my poor algorithm won't work because it needs to know how many for loops should be "hard encoded".Here is my code,hope to see more graceful solution.
Code:
i=0
awk 'BEGIN{RS=","}{print $1}' txt | sed 's/;/ /g' >> a.txt
while read str;do
	eval array${i}=$str
	let "i += 1"
done < a.txt

for a in $array0;do
	for b in $array1;do
		for c in $array2;do
			for d in $array3;do
				for e in $array4;do
					echo $a,$b,$c,$d,$e
				done
			done
		done
	done
done

# 3  
Old 01-31-2011
Hi,
Thanks for responding.
The later part of the script,ie, the section under for seems to be failing.
I used the following script.
Code:
#!/bin/awk -f
i=0
awk 'BEGIN{RS=",";FS="\n"}{print $1}' a.txt | sed 's/;/ /g' >> b.txt
while read str;do
    eval array${i}=$str
    i=`expr $i + 1`
    echo $i
done < b.txt
for a in $array0;do
        for b in $array1;do
                for c in $array2;do
                        for d in $array3;do
                                for e in $array4;do
                                        for f in $array5;do
                                        echo $a,$b,$c,$d,$e,$f
                                done
                        done
                done
          done
     done
done

could you plz suggest why is it failing in the or loop.

Regards
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 01-31-2011 at 10:54 AM.. Reason: code tags, Please!
# 4  
Old 01-31-2011
Quote:
Originally Posted by snukala
Code:
#!/bin/awk -f

you enforce the awk to explain this scripting by using "#! /bin/awk -f".This scripting has a for loop,it's a shell style loop,not awk.It should be #! /bin/sh.Smilie
# 5  
Old 02-01-2011
Code:
awk -F, -v m=\" '{c2=split($2,col2,";");c3=split($3,col3,";");c5=split($5,col5,";")
for(i=0;++i<=c2;)
 for(j=0;++j<=c3;)
  for(k=0;++k<=c5;)
    print $1,col2[i] m,col3[j] m,$4,col5[k] m}' OFS="," infile

Use nawk on solaris...
This User Gave Thanks to malcomex999 For This Post:
# 6  
Old 02-01-2011
Hi malcomex999 and homeboy,
Thank you very much for your support.
It is working.

Regards
Subhash Nukala
# 7  
Old 02-04-2011
Hi malcomex999
Can you please let me know how i can do the reverse.
I mean the following into one line .
Code:
"attr1","amv","bmv1","abc","abc1"
"attr1","amv1","bmv1","abc","abc2"
"attr1","amv1","bmv1","abc","abc3"

"attr1","amv1","bmv2","abc","abc1"
"attr1","amv1","bmv2","abc","abc2"
"attr1","amv1","bmv2","abc","abc3"

To single line like:
Code:
"attr1","amv,av1","bmv1,bmv2","abc","abc1,abc2,abc3"

Thanks and Regards

Moderator's Comments:
Mod Comment Again, please use code tags when posting data and code samples!

Last edited by Scott; 02-04-2011 at 08:26 AM.. Reason: Code tags, again...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To Split the file based on column value

Hi Team, I have a requirement in such a way that need to split the file into two based on which column particular value appears.Please find my sample file below. Lets consider the delimiter of this file as either comma or two colons.(:: and ,). So I need to split the file in such a way that all... (2 Replies)
Discussion started by: ginrkf
2 Replies

2. UNIX for Advanced & Expert Users

Split one file to many based on pattern

Hello All, I have records in a file in a pattern A,B,B,B,B,K,A,B,B,K Is there any command or simple logic I can pull out records into multiple files based on A record? I want output as File1: A,B,B,B,B,K File2: A,B,B,K (9 Replies)
Discussion started by: deal1dealer
9 Replies

3. Shell Programming and Scripting

Split File based on different conditions

I need to split the file Conditions: Ignore any record that either starts with 1 or 9 Split the file at position 404 , if position 404 is abc or def then write all the records in a file > File 1 , the remaining records should go in to a file > File 2 Further I want to split the... (7 Replies)
Discussion started by: protech
7 Replies

4. UNIX for Dummies Questions & Answers

Split file based on column

i have file1.txt asdas|csada|130310|0423|A1|canberra sdasd|sfdsf|130426|2328|A1|sydney Expected output : on eaceh third and fourth colum, split into each two characters asdas|csada|13|03|10|04|23|A1|canberra sdasd|sfdsf|13|04|26|23|28|A1|sydney (10 Replies)
Discussion started by: radius
10 Replies

5. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

6. Shell Programming and Scripting

Split file based on size

Hi Friends, Below is my requirement. I have a file with the below structure. 0001A1.... 0001B1.. .... 0001L1 0002A1 0002B1 ...... 0002L1 .. the first 4 characters are the sequence numbers for a record, A record will start with A1 and end with L1 with same sequence number. Now the... (2 Replies)
Discussion started by: diva_thilak
2 Replies

7. Shell Programming and Scripting

Split the file based on the content

Arun kumar something somehting Enterting in to the line . . . . Some text text Finshing the sentence Some other text . . . . Again something somehting Enterting in to the line . . . . . . Again text text Finshing the sentence (6 Replies)
Discussion started by: arukuku
6 Replies

8. UNIX for Dummies Questions & Answers

Filtering maximum valued file.

Hi a.123 a.136 a.146 B.124 B.39 C.24 I have the files in above format ; I want to list out the files which has maximum value in each group as pipe delimited : a.146|B.124|C.24 Please help me in this. Thanks (1 Reply)
Discussion started by: pandeesh
1 Replies

9. Shell Programming and Scripting

bash: How to split up a file based on another?

I've got these 2 files, FILE.txt and SPLIT_BY.txt: FILE.txt contents: FILE01 FILE02 FILE03 FILE04 FILE05 FILE06 FILE07 FILE08 FILE09 FILE10 FILE11 FILE12 FILE13 FILE14 FILE15SPLIT_BY.txt contents: 2 5 (4 Replies)
Discussion started by: byte711
4 Replies

10. Shell Programming and Scripting

How to split file based on subtitle

Hi, unix Gurus, I want to split file based on sub_title. for example: original file fruit apple watermelon meat pork fish beef expected result file file1 fruit apple watermelon file2 meat pork fish beef. (4 Replies)
Discussion started by: ken002
4 Replies
Login or Register to Ask a Question