如何使用简单的 perl 脚本重新排列我的 txt 文件的行?

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

我原来的txt文件是这样的:

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

如何将其转换为:

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

我试过了

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

它起作用了,但我怎样才能消除“”

您可以使用替换来删除双引号

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

/r returns 值而不是改变值的地方。

#!usr/bin/perl

#Open files
open(FH,'<','rearrange letters.txt');
open(TMP, '>','temp.txt');
# A "a,b,c"
# B "d"
# C "e,f"
#<----This extra new line is necessary for this code to run
while(chomp($ln=<FH>)){
    #Get rid of double quotes
    $ln=~s/\"//g;
    #Take out the non quoted first character and store it in a scalar variable
    my @line = split(' ',$ln);
    my $capletter = shift @line;
    #split the lower case characters and assign them to an array
    my @lowercaselist = split(',',$line[0]);
    #Iterate through the list of lower case characters and print each to the temp file preceded by the capital character
    for my $lcl(@lowercaselist){
        print TMP "$capletter $lcl\n";
    }
}
#Close the files
close(FH);
close(TMP);
#Overwrite the old file with the temp file if that is what you want
rename('temp.txt','rearrange letters.txt');