【问题标题】:perl one line script with conditionperl 带条件的单行脚本
【发布时间】:2017-01-24 23:07:19
【问题描述】:
我有一些文本文件。例如
1;one;111
2;two;222
22;two;222
3;three;333
我尝试使用 perl-oneliner 选择包含“one”的行:
perl -F";" -lane 'print if $F[1]=="one"' forPL.txt
但我从文件中获取所有行。
我不需要使用正则表达式(reg exp 在这种情况下有帮助),我需要在第二个字段上完全匹配。
提前谢谢你
【问题讨论】:
标签:
perl
comparison-operators
【解决方案1】:
使用eq 进行字符串比较,而不是使用== 进行数字比较。
perl -F";" -e 'print if $F[1] eq "one" ' test.txt
编辑:正如 toolic 在他的评论中所建议的那样,如果您使用了警告,您可以很容易地发现问题。
$ 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