带有条件的 perl 一行脚本



我有一些文本文件。

例如
1;one;111
2;two;222
22;two;222
3;three;333

我尝试使用 perl-oneliner 选择包含"一"的行:

perl -F";" -lane 'print if $F[1]=="one"' forPL.txt

但是我从文件中获取所有行。我不需要使用正则表达式(在这种情况下reg exp会有所帮助),我需要在第二个字段上完全匹配。提前谢谢你

使用 eq 进行字符串比较,而不是==用于数字比较。

perl -F";" -e 'print if $F[1] eq "one" ' test.txt

编辑:正如工具在他的评论中建议的那样,如果您使用了警告,您可以轻松发现问题。

$ perl -F";" -e 'use warnings; print if $F[1] == "one" ' test.txt 
Argument "one" isn't numeric in numeric eq (==) at -e line 1, <> line 1.
Argument "one" isn't numeric in numeric eq (==) at -e line 1, <> line 1.
1;one;111
Argument "two" isn't numeric in numeric eq (==) at -e line 1, <> line 2.
2;two;222
Argument "two" isn't numeric in numeric eq (==) at -e line 1, <> line 3.
22;two;222
Argument "three" isn't numeric in numeric eq (==) at -e line 1, <> line 4.
3;three;333

最新更新