perl 用逗号格式化文本

问题描述 投票:0回答:1

我有一个这样格式的文件

0:12 
well good morning everybody thank you for joining us here at the National Shrine of the 
Divine Mercy it is
0:18 
Vietnamese day and uh we're glad that you could join us uh I have a strong tie 
0:24
to the Vietnamese people my father obviously serving in Southeast Asia being in Vietnam and uh my uh Seminary
0:31 
time I went to Seminary with a lot of the Vietnamese sisters so praise be to God uh we're glad you're with us and
0:38 
today's topic really is so important and I'm coming from a aspect of a personal

我想要的格式是这样的

0:12, well good morning everybody thank you for joining us here at the National Shrine of the Divine Mercy it is
0:18, Vietnamese day and uh we're glad that you could join us uh I have a strong tie
0:24, to the Vietnamese people my father obviously serving in Southeast Asia being in Vietnam and uh my uh Seminary
0:31, time I went to Seminary with a lot of the Vietnamese sisters so praise be to God uh we're glad you're with us and
0:38, today's topic really is so important and I'm coming from a aspect of a personal

时间戳将输出到此 1:35:35

#!/usr/bin/perl
use strict;
use warnings;

if (@ARGV != 2) {
    die "Usage: $0 input_file output_file\n";
}
my ($input_file, $output_file) = @ARGV;
open(my $in, '<', $input_file) or die "Cannot open input file '$input_file': $!";
open(my $out, '>', $output_file) or die "Cannot open output file '$output_file': $!";
my $timestamp = '';
while (my $line = <$in>) {
    chomp $line;

    if ($line =~ /^[0-9:]+$/) {
        # Line is a timestamp
        $timestamp = $line;
    } elsif ($line =~ /\S/) {
        # Line is text and is not empty
        print $out "$timestamp, $line\n";
    }
}
close($in);
close($out);

print "Formatting complete. Output written to $output_file.\n";

我写了上面的脚本,但是文件是这样的。 它不应该是

, 0:12
, well good morning everybody thank you for joining us here at the National Shrine of the Divine Mercy it is
, 0:18
, Vietnamese day and uh we're glad that you could join us uh I have a strong tie
, 0:24
, to the Vietnamese people my father obviously serving in Southeast Asia being in Vietnam and uh my uh Seminary
, 0:31
, time I went to Seminary with a lot of the Vietnamese sisters so praise be to God uh we're glad you're with us and
, 0:38
, today's topic really is so important and I'm coming from a aspect of a personal

我也尝试过这个

sed 's/^\([0-9:]*\) \(.*\)$/\1\:\2/'
bash perl format
1个回答
0
投票

根据您的实际输入是什么,解决方案可能很简单:

perl -pwe'chomp if /\d\s*$/' input.txt > output.txt

简单地检查一行是否以数字(和可选的空格)结尾,如果是,则删除换行符。并打印所有内容。 Perl 将读取输入文件,shell 重定向将定向输出。

现在您可能有一些更复杂的东西尚未向我们展示。如果使用 Data::Dumper 的 useqq 选项打印输入文件,您可能会发现新的东西:

use Data::Dumper;
$Data::Dumper::Useqq=1;
print Dumper <$inputfile>;
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.