sedを使用して代替をリンクせずに複数の代替を実行する方法はありますか?

sedを使用して代替をリンクせずに複数の代替を実行する方法はありますか?

AをBBに、BをAAに置き換えるシェルスクリプトを作成したいと思います。この種の仕事に対する私の一般的な解決策は、次のようにsedへの複数の呼び出しをリンクすることですAYBBBYAA

sed 's/A/BB/g;s/B/AA/g'

ただし、この場合はにA翻訳されているため動作しません。また、代替テキストが1文字より長いため、オプションではないようです。私ができることは他にありますか? sed や tr 以外のものを使うかは関係ありません。AAAABBtr

ベストアンサー1

この種の問題では、両方のパターンを同時に検索できるようにループが必要です。

awk '
    BEGIN {
        regex = "A|B"
        map["A"] = "BB"
        map["B"] = "AA"
    }
    {
        str = $0
        result = ""
        while (match(str, regex)) {
            found = substr(str, RSTART, RLENGTH)
            result = result substr(str, 1, RSTART-1) map[found]
            str = substr(str, RSTART+RLENGTH)
        }
        print result str
    }
'

もちろん、Perlが利用可能であれば、それに対応するonelinerがあります:

perl -pe '
    BEGIN { %map = ("A" => "BB", "B" => "AA"); }
    s/(A|B)/$map{$1}/g;
'

パターンに特殊文字が含まれていない場合は、動的に正規表現を作成することもできます。

perl -pe '
    BEGIN {
        %map = ("A" => "BB", "B" => "AA");
        $regex = join "|", keys %map;
    }
    s/($regex)/$map{$1}/g;
'

ところで、Tclにはonelinersという組み込みコマンドがありますが、string mapTcl onelinersを書くのは簡単ではありません。


長さでキーをソートする効果を示します。

  1. ソートされていない

    $ echo ABBA | perl -pe '
        BEGIN {
            %map = (A => "X", BB => "Y", AB => "Z");
            $regex = join "|", map {quotemeta} keys %map;
            print $regex, "\n";
        }
        s/($regex)/$map{$1}/g
    '
    
    A|AB|BB
    XYX
    
  2. 並べ替え

    $ echo ABBA | perl -pe '
          BEGIN {
              %map = (A => "X", BB => "Y", AB => "Z");
              $regex = join "|", map {quotemeta $_->[1]}
                                 reverse sort {$a->[0] <=> $b->[0]}
                                 map {[length, $_]}
                                 keys %map;
              print $regex, "\n";
          }
          s/($regex)/$map{$1}/g
      '
    
    BB|AB|A
    ZBX
    

Perlの「一般的な」並べ替えとSchwartzianの並べ替えベンチマーク:サブルーチンのコードは以下から直接取得されます。sort文書

#!perl
use Benchmark   qw/ timethese cmpthese /;

# make up some key=value data
my $key='a';
for $x (1..10000) {
    push @unsorted,   $key++ . "=" . int(rand(32767));
}

# plain sorting: first by value then by key
sub nonSchwartzian {
    my @sorted = 
        sort { ($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0] || uc($a) cmp uc($b) } 
        @unsorted
}

# using the Schwartzian transform
sub schwartzian {
    my @sorted =
        map  { $_->[0] }
        sort { $b->[1] <=> $a->[1] || $a->[2] cmp $b->[2] }
        map  { [$_, /=(\d+)/, uc($_)] } 
        @unsorted
}

# ensure the subs sort the same way
die "different" unless join(",", nonSchwartzian()) eq join(",", schwartzian());

# benchmark
cmpthese(
    timethese(-10, {
        nonSchwartzian => 'nonSchwartzian()',
        schwartzian    => 'schwartzian()',
    })
);

実行してください:

$ perl benchmark.pl
Benchmark: running nonSchwartzian, schwartzian for at least 10 CPU seconds...
nonSchwartzian: 11 wallclock secs (10.43 usr +  0.05 sys = 10.48 CPU) @  9.73/s (n=102)
schwartzian: 11 wallclock secs (10.13 usr +  0.03 sys = 10.16 CPU) @ 49.11/s (n=499)
                 Rate nonSchwartzian    schwartzian
nonSchwartzian 9.73/s             --           -80%
schwartzian    49.1/s           405%             --

Schwartzian変換を使用するコードは4倍高速です。

比較関数はどこにありますか?ただ length各要素に対して、次の操作を行います。

Benchmark: running nonSchwartzian, schwartzian for at least 10 CPU seconds...
nonSchwartzian: 11 wallclock secs (10.06 usr +  0.03 sys = 10.09 CPU) @ 542.52/s (n=5474)
schwartzian: 10 wallclock secs (10.21 usr +  0.02 sys = 10.23 CPU) @ 191.50/s (n=1959)
                Rate    schwartzian nonSchwartzian
schwartzian    191/s             --           -65%
nonSchwartzian 543/s           183%             --

Schwartzianは、この安価なソート機能を使用すると速度がはるかに遅くなります。

今、悪意のあるコメントから抜け出すことができますか?

おすすめ記事