I have a text file having number of different rows like this..
Action & Adventure|Whiteout|
Kids Free|PBS KIDS Sprout|En espanol|
Kids Free|PBS KIDS Sprout|En espanol|XYZ|
Basically,i want to read the file and write to another file in a tree structure like this..
each row should have 6 columns seperated by |
Action & Adventure||||||
Action & Adventure|Whiteout|||||
Kids Free|||||||
Kids Free|PBS KIDS Sprout|||||
Kids Free|PBS KIDS Sprout|En espanol||||
Kids Free|||||||
Kids Free|PBS KIDS Sprout|||||
Kids Free|PBS KIDS Sprout|En espanol||||
Kids Free|PBS KIDS Sprout|En espanol|xyz|||
sorry for the ambiguity.it's my bad. I have modified it now.
Please help me to write a script
Thaks in advance
Scott
January 21, 2010, 6:04pm
2
Hi ramse8pc.
I really don't understand the question. Can you please elaborate?
very ambiguous question...
$ cat data
Action & Adventure|Whiteout|
Kids Free|PBS KIDS Sprout|En espanol|
Kids Free|PBS KIDS Sprout|En espanol|XYZ|
$ awk -F\| '{for (i=1; i<NF; i++) {for (j=1; j<=6; j++) printf("%s", (j<=i ? $j : "") "|"); printf("\n")}}' data
Action & Adventure||||||
Action & Adventure|Whiteout|||||
Kids Free||||||
Kids Free|PBS KIDS Sprout|||||
Kids Free|PBS KIDS Sprout|En espanol||||
Kids Free||||||
Kids Free|PBS KIDS Sprout|||||
Kids Free|PBS KIDS Sprout|En espanol||||
Kids Free|PBS KIDS Sprout|En espanol|XYZ|||
Regards,
Alister
hello,
localhost:/home/gaurav# echo 'Kids Free|PBS KIDS Sprout|En espanol|
Kids Free|PBS KIDS Sprout|En espanol|XYZ|'
Kids Free|PBS KIDS Sprout|En espanol|
Kids Free|PBS KIDS Sprout|En espanol|XYZ|
localhost:/home/gaurav# echo 'Kids Free|PBS KIDS Sprout|En espanol|
Kids Free|PBS KIDS Sprout|En espanol|XYZ|' | perl -wn -e 'my @arr=split(/\|/);for($i=0;$i<=$#arr;$i++){for($j=0;$j<$i;$j++){print $arr[$j],"|"}print "|||||\n";}'
|||||
Kids Free||||||
Kids Free|PBS KIDS Sprout||||||
Kids Free|PBS KIDS Sprout|En espanol||||||
|||||
Kids Free||||||
Kids Free|PBS KIDS Sprout||||||
Kids Free|PBS KIDS Sprout|En espanol||||||
Kids Free|PBS KIDS Sprout|En espanol|XYZ||||||
localhost:/home/gaurav#
Regards,
Gaurav.
awk is not working though.. i tried like this in ksh file..
awk -F"|" '{for (i=1; i<$NF; i++){for (j=1; j<=6; j++)printf("%s", (j<=i ? $j : "") "|");printf("\n")}}' FFolderVHO.txt
awk: syntax error near line 1
awk: illegal statement near line 1
what's wrong with it?
Scott
January 23, 2010, 9:09am
7
Hi ramse8pc.
The awk work perfectly, but if you are using Solaris, use nawk, or /usr/xpg4/bin/awk.
Another one:
awk -F"|" '{s="";for(i=1;i<NF;i++){s=s?s FS $i:$i;print s "||||"}}' file
Use nawk or /usr/xpg4/bin/awk on Solaris