Search code examples
perlparsingtext-parsing

How to rearrange the row of my txt file using simple perl script?


My original txt file is like:

A "a,b,c"
B "d"
C "e,f"

How do I convert it to:

A a
A b
A c
B d
C e
C f

I tried this

perl -ane '@s=split(/\,/, $F[1]); foreach $k (@s){print "$F[0] $k\n";}' txt.txt

It worked but how can I eliminate the " "


Solution

  • You can use substitution to remove the double quotes

    @s = split /,/, $F[1] =~ s/"//gr;
    

    The /r returns the value instead of changing the value in place.