split single line into two line or three lines

Dear All,

I want to split single line into two line or three lines wherever �|� separated values comes using

Input line

test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,

output shoule be

test,DEMTEMPUT20100404010012,,,,,,,,0070086,
test,DEMTEMPUT20100404010012,,,,,,,,0070087,

[/FONT][/SIZE]

awk -F '|' '{for(i=2;i<=NF;i++){ print $1.$i}}' filename

HTH,
PL

Getting following Error

awk -F '|' '{for(i=2;i<=NF;i++){ print $1.$i}}' test1 

 syntax error The source line is 1.
 The error context is
                {for(i=2;i<=NF;i++){ print >>>  $1. <<< $i}}
 awk: The statement cannot be correctly parsed.
 The source line is 1.

try...

 awk -F"|" '{print $1$2",","\n"$1$3}' file
[root@sistem1lnx tes1]# cat 2
test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,

[root@sistem1lnx tes1]# sed -e 's/\([[:graph:]][[:graph:]].*\)/\1\n\1/' -e 's/\([[:graph:]][[:graph:]]*\)|\([0-9][0-9]*\)|\([0-9][0-9]*\)/\1\2/' -e 's/\([[:graph:]][[:graph:]]*\)|\([0-9][0-9]*\)|\([0-9][0-9]*\)/\1\3/' 2
test,DEMTEMPUT20100404010012,,,,,,,,0070086,
test,DEMTEMPUT20100404010012,,,,,,,,0070087,

Something like this: ?

 
awk -F"|" '{ for ( i=2;i<=NF;i++) $i=$1","$i"\n" ;$1=""; print $0 }' inputfile

And now it's time for a Perl solution... :wink:

$
$
$ cat -n f9
     1  test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,
     2  PQRS,abcdefghi201001019999998877,,,,|1234567|8901234|5678901,
$
$
$ perl -lne 'if(/^(.*?)\|([\d|]+)(.*)$/){print "$1$_$3" foreach split/\|/,$2}' f9
test,DEMTEMPUT20100404010012,,,,,,,,0070086,
test,DEMTEMPUT20100404010012,,,,,,,,0070087,
PQRS,abcdefghi201001019999998877,,,,1234567,
PQRS,abcdefghi201001019999998877,,,,8901234,
PQRS,abcdefghi201001019999998877,,,,5678901,
$
$

tyler_durden

$ echo 'test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,' | sed "s/\([^|]*\)|\([^|]*\)|\(.*\)/\1\2,\\
> \1\3/"
test,DEMTEMPUT20100404010012,,,,,,,,0070086,
test,DEMTEMPUT20100404010012,,,,,,,,0070087,

is this posible to get last string in each line??

my input is

test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,true#flase#no

output should be

test,DEMTEMPUT20100404010012,,,,,,,,0070086,true#flase#no
test,DEMTEMPUT20100404010012,,,,,,,,0070087,true#flase#no

You can use also same my code..

[root@sistem1lnx ~]# a="test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,true#flase#no"
 
[root@sistem1lnx ~]# echo $a | sed 's/\([[:graph:]][[:graph:]].*\)/\1\n\1/
s/\([[:graph:]][[:graph:]]*\)|\([0-9][0-9]*\)|\([0-9][0-9]*\)/\1\2/
s/\([[:graph:]][[:graph:]]*\)|\([0-9][0-9]*\)|\([0-9][0-9]*\)/\1\3/'
 
test,DEMTEMPUT20100404010012,,,,,,,,0070086,true#flase#no
test,DEMTEMPUT20100404010012,,,,,,,,0070087,true#flase#no

The Perl script posted earlier should work for this as well, since everything after the digits and "|" character is stored in $3. So $3 was just "," for the former input file and it would be ",true#false#no" for this input file.

$
$
$ cat -n f99
     1  test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087,true#false#no
     2  PQRS,abcdefghi201001019999998877,,,,|1234567|8901234|5678901,true#false#no
$
$ perl -lne 'if(/^(.*?)\|([\d|]+)(.*)$/){print "$1$_$3" foreach split/\|/,$2}' f99
test,DEMTEMPUT20100404010012,,,,,,,,0070086,true#false#no
test,DEMTEMPUT20100404010012,,,,,,,,0070087,true#false#no
PQRS,abcdefghi201001019999998877,,,,1234567,true#false#no
PQRS,abcdefghi201001019999998877,,,,8901234,true#false#no
PQRS,abcdefghi201001019999998877,,,,5678901,true#false#no
$
$

tyler_durden

sed "s/\([^|]*\)|\([^|]*\)|\([^,]*\),\(.*\)/\1\2,\4\\
> \1\3,\4/" file

it not working when Alphanumeric value is comming

cat test1
test,DEMTEMPUT20100404010012,,,,,,,,|0a70086|007c087,true#false#no
PQRS,abcdefghi201001019999998877,,,,|1234567|8901234|5678901,true#false#no
AAAS,abRRRRRhi2010010199900000,,,,|123c567|89a1234,true#false#no
 
output is 
perl -lne 'if(/^(.*?)\|([\d|]+)(.*)$/){print "$1$_$3" foreach split/\|/,$2>
test,DEMTEMPUT20100404010012,,,,,,,,0a70086|007c087,true#false#no
PQRS,abcdefghi201001019999998877,,,,1234567,true#false#no
PQRS,abcdefghi201001019999998877,,,,8901234,true#false#no
PQRS,abcdefghi201001019999998877,,,,5678901,true#false#no
AAAS,abRRRRRhi2010010199900000,,,,123c567|89a1234,true#false#no
 
 
 

This should work in that case:

$ 
$ cat test1
test,DEMTEMPUT20100404010012,,,,,,,,|0a70086|007c087,true#false#no
PQRS,abcdefghi201001019999998877,,,,|1234567|8901234|5678901,true#false#no
AAAS,abRRRRRhi2010010199900000,,,,|123c567|89a1234,true#false#no
$ 
$ 
$ perl -lne 'if(/^(.*?)\|([\w|]+)(.*)$/){print "$1$_$3" foreach split/\|/,$2}' test1
test,DEMTEMPUT20100404010012,,,,,,,,0a70086,true#false#no
test,DEMTEMPUT20100404010012,,,,,,,,007c087,true#false#no
PQRS,abcdefghi201001019999998877,,,,1234567,true#false#no
PQRS,abcdefghi201001019999998877,,,,8901234,true#false#no
PQRS,abcdefghi201001019999998877,,,,5678901,true#false#no
AAAS,abRRRRRhi2010010199900000,,,,123c567,true#false#no
AAAS,abRRRRRhi2010010199900000,,,,89a1234,true#false#no
$ 
$ 

tyler_durden

its Working!!

Thank you :slight_smile: