Shrikant Shelar Shrikant Shelar - 4 months ago 15
Perl Question

i want code in PERL for splitting a string with multiple delimiter

I have a string like this

LOADID##49945-19-0-3540FAA-16995-16995%%ANAME##NWADVE15%%AID##51826%%AGNAME##ADDLEXPR%%AGID##49945%%HNAME##teranetcmoduat.teraner.com%%LOADDATE##07-12-16%%LOADTIME##02-04-53.end


I have to extract values for parameters

LOADID, ANAME,AID,AGNAME,AGID, HNAME, LOADDATE, LOADTIME


and write it into text file.
output format is

49945-19-0-3540FAA-16995-16995|NWADVE15|51826|ADDLEXPR|49945|teranetcmoduat.teraner.com|07-12-16|02-04-53

Answer

Just a simple Perl script with 2 regex replacements will do
(tested with Perl 5)

$ cat input.txt
LOADID##49945-19-0-3540FAA-16995-16995%%ANAME##NWADVE15%%AID##51826%%AGNAME##ADDLEXPR%%AGID##49945%%HNAME##teranetcmoduat.teraner.com%%LOADDATE##07-12-16%%LOADTIME##02-04-53.end

$ echo 's/(.*?)##(.*?)(?:%%|.end|$)/$2|/g;s/\|$//' > pipeitup.pl

$ perl -p pipeitup.pl input.txt > result.txt

$ cat result.txt
49945-19-0-3540FAA-16995-16995|NWADVE15|51826|ADDLEXPR|49945|teranetcmoduat.teraner.com|07-12-16|02-04-53

The second regex replace s/\|$// is just added to remove the pipe at the end.
So if a final pipe doesn't bother you, then just then you can golf it down to only the first regex replace s/(.*?)##(.*?)(?:%%|.end|$)/$2|/g.