Shrikant Shelar Shrikant Shelar - 4 months ago 21
Perl Question

I want code in Perl for splitting a string with multiple delimiters

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 the 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 you can just golf it down to only the first regex replace s/(.*?)##(.*?)(?:%%|\.end$|$)/$2|/g.

And btw, just with a minor change you can print a header with the field names. (since those names are in capture group 1 of the first regex)

$ head -n1 input.txt |perl -p -e 's/(.*?)##(.*?)(?:%%|\.end$|$)/$1|/g;s/\|$//' >header.txt