Computing.Net > Forums > Unix > spliting the rows

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to get for your free account now!

spliting the rows

Reply to Message Icon

Name: dprakash
Date: December 15, 2008 at 03:41:45 Pacific
OS: DF
CPU/Ram: DF
Manufacturer/Model: Df / DF
Comment:

Hi everybody...
Please help me regarding this issue asap.
we have requirement ... source file contains a column contains a id (primary key) and other(2nd) column which is having one or more values like (toys,car,bus) here comma is not a filed delimeter and . Filed delimiter is a '|'. for example
Sample input:

100|toys,car,bus
200|hat,soap,kite,lux
300|jugs,mug

Sample output should be like this:
100,toys
100,cars
100,bus
200,hat
200,soap
200,kite
200,lux
300,jugs
300,mug

2nd column may contain values dynamically......
1st column is integer and 2nd column is longvarchar(2000)....
please help me regarding this....
Thanks
Prakash


Report Offensive Message For Removal

Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: December 15, 2008 at 10:09:15 Pacific
Reply:

I'm assuming you meant to say that after parsing the data into 2 columns by pipe symbol that the second column was then parsed by commas.

Maybe the script below will do what you want.
The only hard part is this:

eval echo \"\$${i}\"

As var i changes, this evaluates and prints the contents of ${i}

Let me know if you have any questions.


#!/bin/ksh

i=2
while IFS="|" read col1 col2
do
# parse column 2 by commas
set - $(IFS=","; echo ${col2})
i=1
while (($i <= $#))
do
printf "%d,%s\n" $col1 $(eval echo \"\$${i}\")
((i+=1))
done
done < datafile


Report Offensive Follow Up For Removal
Reply to Message Icon

Related Posts

See More







Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: spliting the rows

Adding variable at end of the row www.computing.net/answers/unix/adding-variable-at-end-of-the-row/7466.html

deleting rows in a file www.computing.net/answers/unix/deleting-rows-in-a-file/3676.html

displaying words in a row to column www.computing.net/answers/unix/displaying-words-in-a-row-to-column/4945.html