gpt4 book ai didi

linux - Perl 将文件中的行放入单个文件中

转载 作者:可可西里 更新时间:2023-11-01 11:50:10 25 4
gpt4 key购买 nike

我有一个名为 mail.txt 的文件,其中打印的行如下所示,我想将所有这些行放在一行中,如下所示

谢谢


这是输入

q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>                     (host map: lookup (my.local.domain): deferred)                                             <yagyavalkbhatt@yahoo.com>                                             <ygyalkatt@yahoo.com>q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>                     (host map: lookup (my.local.domain): deferred)                                             <yagyavalkbhatt@yahoo.com>                                             <yagyav@yahoo.com>

这是输出

q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>,(host map: lookup (my.local.domain): deferred),<yagyavalkbhatt@yahoo.com>,<ygyalkatt@yahoo.com>q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>,(host map: lookup (my.local.domain): deferred), <yagyavalkbhatt@yahoo.com>,<yagyav@yahoo.com>

最佳答案

那你为什么不直接去做呢?

open(my $fh, "<", $input_filename);

my @lines = map { chomp; $_} <$fh>; #1

close $fh;

open(my $out, ">", $output_filename);

print $out join "", @lines; # or maybe a different separator, like ","

close $out;

#that's it

注意:如果你想去除输入行开头和结尾的多余空格,你可以将 #1 行替换为

my @lines = map { s/\s+$//; s/^\s+//; $_} <$fh>;

关于linux - Perl 将文件中的行放入单个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10910714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com