Perl To Parse Whitespace Separated Columns

时间:2016-07-20 14:29:50

标签: perl parsing whitespace

我有一个大文本文件,其中有三列,每列用四个空格分隔。我需要一个perl脚本来读取这个文本文件,并将#1和#2列输出到一个新的文本文件中,每个列用引号括起来,并在输出文件中用逗号分隔。

包含四列的文本文件包含如下所示的数据:

    9a2ba3c0580b5f3799ad9d6f487b2d3    /folder1/folder2/folder3/folder4/folder5/folder6/folder7_name_PC/images/filename.jpg    HOST

我希望输出看起来像

"9a2ba3c0580b5f3799ad9d6f487b2d38","/folder1/folder2/folder3/folder4/folder5/folder6/folder7_name_PC/images/filename.jpg"

3 个答案:

答案 0 :(得分:2)

简单易行:

perl -lane 'print join ",", map qq("$_"), @F[0, 1]'
  • -l处理print
  • 中的换行符
  • -n逐行读取输入行
  • -a将空格上的每一行拆分为@F数组
  • @F[0, 1]是一个数组切片,它提取@F数组的前两个元素
  • map用双引号
  • 包装每个元素
  • join
  • 之间插入逗号

答案 1 :(得分:0)

以下代码供您参考:

#!/usr/bin/perl
my $defaultFileName=defined $ARGV[0]?$ARGV[0]:"filename.txt";
die "Could not find file: $defaultFileName" unless(-f $defaultFileName);
open my $fh, '<',"textFileName.log";
foreach my $line(<$fh>) {
    my @tmpData=split(/\s+/, $line);
    printf "\"%s\",\"%s\"\\n\n",$tmpData[1],$tmpData[2];
}
close $fh;

答案 2 :(得分:-1)

这也可以使用awk

完成
>>cat test
9a2ba3c0580b5f3799ad9d6f487b2d3    /folder1/folder2/folder3/folder4/folder5/folder6/folder7_name_PC/images/filename.jpg    HOST
9a2ba3c0580b5f3799ad9d6f487b2d3    /folder1/folder2/folder3/folder4/folder5/folder6/folder7_name_PC/images/filename.jpg    HOST
9a2ba3c0580b5f3799ad9d6f487b2d3    /folder1/folder2/folder3/folder4/folder5/folder6/folder7_name_PC/images/filename.jpg    HOST
9a2ba3c0580b5f3799ad9d6f487b2d3    /folder1/folder2/folder3/folder4/folder5/folder6/folder7_name_PC/images/filename.jpg    HOST

<强>输出:

>>awk '{FS="    "}{print "\""$1"\",""\""$2"\",""\""$3"\"" }' test
"9a2ba3c0580b5f3799ad9d6f487b2d3","/folder1/folder2/folder3/folder4/folder5/folder6/folder7_name_PC/images/filename.jpg","HOST"
"9a2ba3c0580b5f3799ad9d6f487b2d3","/folder1/folder2/folder3/folder4/folder5/folder6/folder7_name_PC/images/filename.jpg","HOST"
"9a2ba3c0580b5f3799ad9d6f487b2d3","/folder1/folder2/folder3/folder4/folder5/folder6/folder7_name_PC/images/filename.jpg","HOST"
"9a2ba3c0580b5f3799ad9d6f487b2d3","/folder1/folder2/folder3/folder4/folder5/folder6/folder7_name_PC/images/filename.jpg","HOST"

>>awk '{FS="    "}{print "\""$1"\",""\""$2"\",""\""$3"\"" }' test > output.txt

然后output.txt将有所需的输出。