Why is my -p liner printing lines I'm trying to skip?

I have a file with a name with three lines:

line one
line second
line three

When I do this:

perl -ne 'print if / one /' file

I get this output:

line one

when i try this:

perl -pe 'next if / one /' file

conclusion:

line one
row second
String tree

I expected the same output as with a single layer. Are my expectations wrong or is something wrong?

+3
source share
3 answers

Your expectation is wrong. The switch -pplaces the following loop around your code:

LINE:
  while (<>) {
      ...             # your program goes here
  } continue {
      print or die "-p destination: $!\n";
  }

If you are reading the documentation fornext , it says:

, continue , .

next ( ), .

-

perl -pe '$_ = "" unless /one/' file
+8

, :

-n    assume "while (<>) { ... }" loop around program
-p    assume loop like -n but print line also, like sed

.

perl -ne 'print if /one/' file

"" .

perl -ne 'next unless /one/' file

, -p.

perl -pe 'next unless /one/' file

, , , - -p .

+3
$ perl -ne 'next unless /one/ && print' file
line one
+1
source

Source: https://habr.com/ru/post/1735768/


All Articles