Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Suppose I have a file that is comma delimited, but then certain fields contain multiple entries using a different delimiter ( in the example below the second delimiter of field 2 is a semi-colon). What perl one-liner at bash prompt would turn the following example data from
ftsI,DB00303 HDC,DB00114; DB00117 F13A1,DB01839; DB02340; DB11311; DB13151

into

ftsI,DB00303 HDC,DB00114 HDC,DB00117 F13A1,DB01839 F13A1,DB02340 F13A1,DB11311 F13A1,DB13151

The point of this is to go from a one to many mapping line by line, to a one to one mapping line by line ( at least with respect to the fields in question.

Something like the following is what I am looking for ... I know this syntax is wrong but hoping that it will get the conceptual point across....

cat file|cut-f1,10|perl -pe 'foreach $entry (@F[1]){print $F[0] . "\t" . $entry . "\n";}'

Replies are listed 'Best First'.
Re: Perl one-liner for array at bash shell
by Anonymous Monk on Aug 22, 2017 at 21:57 UTC
    Or use Text::CSV... not as short but it'll handle different input formats better
    perl -MText::CSV=csv -e 'for $a (@{csv(in=>"input.csv")}) { csv(in=>[ map[@$a,$_],@{csv(in=>\pop@$a,sep=>";", allow_whitespace=>1)->[0]} ]) }'
Re: Perl one-liner for array at bash shell
by Anonymous Monk on Aug 22, 2017 at 20:50 UTC
    perl -naF, -le 'print "$F[0],$_" for split /[;\s]+/,$F[1]'
      Awesome works perfectly. One follow on questions... say I wanted to print the entire line except instead of just $F[0].... the $_ variable now pertains to the split function, so how to print the whole line? Thanks!!
        Don't get it, show what output you want?
Re: Perl one-liner for array at bash shell
by Anonymous Monk on Aug 22, 2017 at 21:01 UTC
    TIMTOWTDI
    perl -ple 's/[;\s]+/m#.*,#;$\.$&/eg'
      Ok this works perfectly too... what does the m# the ,# and the $& stand for here? Love perl special chars !
        m#.*,# is the same as /.*,/ but the slashes were already used by s///, and the vars are $\ (set by perl -l to "\n", see perlrun) and $&. Same thing:
        perl -ple 's{ [;\s]+ }{ m{.*,}; $/ . $& }egx' perl -ple '($a)=/(.*,)/; s/[;\s]+/\n$a/g'