http://www.perlmonks.org?node_id=11145182

abdan has asked for the wisdom of the Perl Monks concerning the following question:

How do we have CLI of Perl's sed's n command ?
e.g. illustration:

cat script.txt| perl -nle 'if (/^===\w+\s*$/){ next # ??? how to ignor +e first $_, here eg. ===Hello, to directly become replaced by next li +ne ; # ... } print'

Replies are listed 'Best First'.
Re: Must have CLI of Perl's sed's n command
by Tux (Canon) on Jun 30, 2022 at 06:35 UTC

    sed - Perl conversion done with s2p

    =head1 NAME psed - a stream editor =head1 SYNOPSIS psed [-an] script [file ...] psed [-an] [-e script] [-f script-file] [file ...] s2p [-an] [-e script] [-f script-file] =head1 DESCRIPTION A stream editor reads the input stream consisting of the specified fil +es (or standard input, if none are given), processes is line by line by applying a script consisting of edit commands, and writes resulting li +nes to standard output. The filename 'C<->' may be used to read standard i +nput. The edit script is composed from arguments of B<-e> options and script-files, in the given order. A single script argument may be spec +ified as the first parameter. If this program is invoked with the name F<s2p>, it will act as a sed-to-Perl translator. See L<"SED SCRIPT TRANSLATION">. B<sed> returns an exit code of 0 on success or >0 if an error occurred +.

    Enjoy, Have FUN! H.Merijn
      How is usage ?
      $ ./s2p -e '/^```\w+$/{n; :a N; /\n```$/!ba }' bash: ./s2p: perl: bad interpreter: No such file or directory $ ./s2p bash: ./s2p: perl: bad interpreter: No such file or directory
      keep such error
        What operating system? Whats the first line of s2p? Why are you calling ./s2p instead of s2p, how did you install it?
Re: Must have CLI of Perl's sed's n command
by Fletch (Bishop) on Jun 30, 2022 at 02:02 UTC

    Your formatting is kind of terrible, but you can check $. and skip if the first line (next if $. == 1). Another option might be to use the trick of using -M and a version to get something to run before the magical while wrapping the -n does. Use something like -M5.010’;scalar <>;’ to have the first line read (and discarded).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      > Your formatting is kind of terrible,

      yep, that's one way to express it.

      > skip if the first line (next if $. == 1)

      If that's the goal (???) $.-1 is false for the first line only, he could && it with the flip-flop's start condition.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      seems we to conclude there's none, as
      next if $_ == ...
      is different; costlier & less eff. - i got/knew it in first place
        $_ and $. are not the same thing.

        Could you show the sed example you're trying to mimic? I'm not sure I understand.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Must have CLI of Perl's sed's n command
by haukex (Archbishop) on Jun 30, 2022 at 07:23 UTC

    Please show the sed script you're trying to replace along with some sample input.