如何指示 Perl 将在特定字符上断开的多行视为单行?

发布于 2024-11-09 13:21:18 字数 218 浏览 0 评论 0原文

例如,我有一个 txt 文件,其中多行调用在 & 符号上断开。

command1

command2

execute myscript &
        opt1=val1 &
        opt2=val2

...

打开文件时,有没有办法告诉 Perl 将这三行视为一行并忽略 &

For example, I have a txt file with multi line calls broken on an ampersand.

command1

command2

execute myscript &
        opt1=val1 &
        opt2=val2

...

While opening the file, is there a way to tell Perl to consider these three lines as if it were a single line and ignore the &?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

信仰 2024-11-16 13:21:18

打开文件时没有。但在阅读时加入他们并不难:

open(my $in, '<', 'file.txt') or die;
while (<$in>) {
  $_ .= <$in> while s/&\s*\z//;

  # $_ now contains a complete record
  ...
}

Not when opening the file. But it's not too hard to join them while reading:

open(my $in, '<', 'file.txt') or die;
while (<$in>) {
  $_ .= <$in> while s/&\s*\z//;

  # $_ now contains a complete record
  ...
}
无戏配角 2024-11-16 13:21:18

如果记录之间总是有多个换行符,请考虑使用记录分隔符来读取它们。然后,您可以快速检查 &,并执行拆分/连接:

use English '-no_match_vars'; 

sub read_records {
    local $RS = "\n\n";  # or,  for the machoistic,  $/ works too without English
    ... # open the file
    while (my $record = <$fh>) {
        chomp $record;               # uses $RS for what to remove, nice!
        if ($record =~ /&\s*$/ms) {  # & at the end of *any* line (w/only spaces)
            $record = join ' ', split /\s*&\s+/, $record; # pull them out
        }
        ... # do something with the record
    }
}

If you always have multiple newlines between records, consider using the Record Separator to read them. Then, you can use a quick check for the &, and perform a split/join:

use English '-no_match_vars'; 

sub read_records {
    local $RS = "\n\n";  # or,  for the machoistic,  $/ works too without English
    ... # open the file
    while (my $record = <$fh>) {
        chomp $record;               # uses $RS for what to remove, nice!
        if ($record =~ /&\s*$/ms) {  # & at the end of *any* line (w/only spaces)
            $record = join ' ', split /\s*&\s+/, $record; # pull them out
        }
        ... # do something with the record
    }
}
凶凌 2024-11-16 13:21:18

我假设您的输入具有合理的大小,因此将整个内容读入标量,清理它,然后处理更友好的结果。

#! /usr/bin/env perl

use strict;
use warnings;

sub read_input {
  my($fh) = @_;
  local $/;
  scalar <$fh>;
}

my $commands = read_input \*DATA;

$commands =~ s/&\n//g;
print $commands;

__DATA__
command1

command2

execute myscript &
        opt1=val1 &
        opt2=val2

输出:

command1

command2

execute myscript         opt1=val1         opt2=val2

I assume your input is of a reasonable size, so read the whole thing into a scalar, clean it up, and then process the friendlier result.

#! /usr/bin/env perl

use strict;
use warnings;

sub read_input {
  my($fh) = @_;
  local $/;
  scalar <$fh>;
}

my $commands = read_input \*DATA;

$commands =~ s/&\n//g;
print $commands;

__DATA__
command1

command2

execute myscript &
        opt1=val1 &
        opt2=val2

Output:

command1

command2

execute myscript         opt1=val1         opt2=val2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文