String manipulation in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String manipulation in a file
# 1  
Old 01-21-2010
String manipulation in a file

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

Last edited by ramse8pc; 01-21-2010 at 08:27 PM.. Reason: Explained clearly
# 2  
Old 01-21-2010
Hi ramse8pc.

I really don't understand the question. Can you please elaborate?
# 3  
Old 01-21-2010
very ambiguous question...
# 4  
Old 01-21-2010
Code:
$ 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
# 5  
Old 01-21-2010
hello,

Code:
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.
# 6  
Old 01-22-2010
awk is not working though.. i tried like this in ksh file..
Code:
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?

Last edited by Scott; 01-23-2010 at 10:06 AM.. Reason: Added code tags
# 7  
Old 01-23-2010
Hi ramse8pc.

The awk work perfectly, but if you are using Solaris, use nawk, or /usr/xpg4/bin/awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

String Manipulation in a text file

Hi I have a requirement to write a script but not sure which is the best way to approach this I have not worked in sed but I'm aware that its robust for file extraction requirements I have a scripting task. I already developed the code in perl but the script is taking almost 2 mins for... (5 Replies)
Discussion started by: John Trevor
5 Replies

2. Shell Programming and Scripting

Awk to convert a text file to CSV file with some string manipulation

Hi , I have a simple text file with contents as below: 12345678900 971,76 4234560890 22345678900 5971,72 5234560990 32345678900 71,12 6234560190 the new csv-file should be like: Column1;Column2;Column3;Column4;Column5 123456;78900;971,76;423456;0890... (9 Replies)
Discussion started by: FreddyDaKing
9 Replies

3. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

4. Shell Programming and Scripting

string manipulation

hi all, i am new to shell scripting and need help. i have a string that stores the month in Jan/Feb/Mar format. i need to convert it to number like 01 for jan, 12 for dec etc. i am using the following sed command, echo "Enter a Month eg. Jan/Feb : " read MONTHEND ... (9 Replies)
Discussion started by: anupom2000
9 Replies

5. Homework & Coursework Questions

String Manipulation

Write a shell program to display the position of the right - most character in a given input string. Example : Input : RAHUL Output : L is in the 5th position also tell me how to count length of string and how to find the position of specific character in left most side. Homework... (0 Replies)
Discussion started by: shashwat2691
0 Replies

6. Shell Programming and Scripting

string manipulation

Hi all, see i have a script that takes few arguments. first one is command we do on file, next is file (mostly txt file with lot of data) third is destination where we do something with data in file. Since im new in scripting, and im learning as i go, i need some hint how to manipulate that... (3 Replies)
Discussion started by: ajemrunner
3 Replies

7. UNIX for Dummies Questions & Answers

String manipulation

I am doing some training for a job I have just got and there is an exercise I am stuck with. I am not posting to ask a question about logic, just a trivial help with string manipulation. I would appreciate if somebody could at least give me a hint on how to do it. Basically, the intelligent part... (8 Replies)
Discussion started by: Dantastik
8 Replies

8. UNIX for Dummies Questions & Answers

string manipulation

Hi, I have a file with rows of text like so : E100005568374098100000015667 D100005568374032000000112682 H100005228374060800000002430 I need to grab just the last digits(bolded) of each line without the proceeding text/numbers. Thanks (5 Replies)
Discussion started by: james6
5 Replies

9. Shell Programming and Scripting

string manipulation

Hi, I have searched this long and hard and don't seem to see another post on this issue. I have two strings each with the same characters but in a different order. String1=”word” String2=”dwor” I want to test them to show their similarity. Unfortunately I can't do a sort so that they will... (9 Replies)
Discussion started by: Cactus Jack
9 Replies

10. UNIX for Dummies Questions & Answers

String manipulation

Hi all, Please help me decide this problem. I have a converter application getting its parameters by specified format. Ie: -date2007-02-14 Also I have a files named yyyyMMdd.rar format. So how do I convert the file name to it. Input: 20070214.rar Output: -date2007-02-14 I wrote the... (2 Replies)
Discussion started by: mr_bold
2 Replies
Login or Register to Ask a Question