perl - replace every nth (and multiples) occurrences of a character with another character

后端 未结 6 1002
余生分开走
余生分开走 2021-01-20 19:27

Does anyone know any unix commands/perl script that would insert a specific character (that can be entered as either hex (ie 7C) or as the actual character (ie |)) in the po

6条回答
  •  耶瑟儿~
    2021-01-20 20:10

    # Get params and create part of the regex.
    my $delim   = "\\" . shift;
    my $n       = shift;
    my $repl    = shift;
    my $wild    = '.*?';
    my $pattern = ($wild . $delim) x ($n - 1);
    
    # Slurp.
    $/       = undef;
    my $text = <>;
    
    # Replace and print.
    $text =~ s/($pattern$wild)$delim/$1$repl/sg;
    print $text;
    

提交回复
热议问题