split content and write to new record

Hi,

Help required to split record value and write to new row.

Input
a~b~c~value in ('3','4','5')~test

output
a~b~c~3~test
a~b~c~4~test
a~b~c~5~test

input
a~b~c~value in ('3','4')~test

output
a~b~c~3~test
a~b~c~4~test

perl -nle '/(.*)value.*(~[^~]+)$/;$a=$1;$b=$2;while (/\d+/g) {print "$a$&$b"}' file

Thanks. This is working fine. But values will be dynamic.

sample input:

a~b~c~value in ('3','4','5')~test2
x~y~val~value in ('0','1','2')~test2

after third delimiter values should be splited.

---------- Post updated 02-14-12 at 06:47 AM ---------- Previous update was 02-13-12 at 12:41 PM ----------

Help is highly appreciated.!

may be code looks ugly, but it do what you want

 
$ cat test.sh
while read line; 
do 
        ou=`echo $line | sed -e 's/.* (\('.*'\))~.*/\1/' -e "s#[\'\,]##g"`
        first=`echo $line | nawk -F~ 'BEGIN{OFS="~"}{print $1,$2,$3}'`
        last=`echo $line | nawk -F~ '{print $NF}'`
        echo $ou | while read -n 1 char; do echo "$first~"$char"~$last"; done
done  < test.txt | sed '/\~\~/d'
$ 
$ 
$ 
$ cat test.txt 
a~b~c~value in ('3','4','5')~test2
x~y~val~value in ('0','1','2')~test2
$ 
$ 
$ 
$ ./test.sh 
a~b~c~3~test2
a~b~c~4~test2
a~b~c~5~test2
x~y~val~0~test2
x~y~val~1~test2
x~y~val~2~test2

hi Kamaraj,

I am getting the below error mesg:

test.sh[6]: read: A specified flag is not valid for this command.

Try:

awk -F\~ '{m=split ($4,T,/'\''/);for(i=2;i<=m;i+=2)print $1,$2,$3,T,$5}' OFS=\~
$
$ cat f59
a~b~c~value in ('3','4','5')~test2
x~y~val~value in ('0','1','2')~test2
p~q~r~value in ('1','2','3','4','5','6','7','8','9','0')~test3
j~k~l~value in ('7')~test4
$
$
$ perl -lne '/^((?:.*?~){3}).*\((.*?)\)(.*)/; ($x,$y)=($1,$3); map{s/\D//g;print $x,$_,$y}split/,/,$2' f59
a~b~c~3~test2
a~b~c~4~test2
a~b~c~5~test2
x~y~val~0~test2
x~y~val~1~test2
x~y~val~2~test2
p~q~r~1~test3
p~q~r~2~test3
p~q~r~3~test3
p~q~r~4~test3
p~q~r~5~test3
p~q~r~6~test3
p~q~r~7~test3
p~q~r~8~test3
p~q~r~9~test3
p~q~r~0~test3
j~k~l~7~test4
$
$

tyler_durden

Thanks Scrutinizer & tyler_durden.

Scrut, It will be great if you explain how it is working.