I am filtering for lines with more than 3 items with
perl -ne 'print if split > 2' file.txt
But when I want to filter for lines with less than 3 items I can't use
perl -ne 'print if split < 2' file.txt
#Unterminated <> operator at -e line 1.
I can work around it with either of these
perl -ne 'print if not split > 2' file.txt
perl -ne 'print if 2 > split' file.txt
But I am wondering why the expression only fails for one of <
,>
.
After a bit of digging, and looking at the comments on your post, I think I've found the answer. The parser is attempting to understand what you mean when it sees split <
. Without parentheses on your call to split, it needs to guess how many arguments you are passing to split (It takes 0-3, see perldoc -f split
for more information).
Because of this, it seems to assume by default that you are trying to call it with at least one argument, which looks like the start of the null filehandle <>
. When it sees that it is incomplete, it prints the error and aborts.
This explains why the fixes commented on your post work. They all make it clear that split is to be called with no arguments, and the parser should not look for an expression, and the <
character can then be treated as an operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With