user2586648
user2586648

Reputation: 19

perl one line script with condition

I have some text file. E.g.

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

I try to select line that contains "one" using perl-oneliner:

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

But I get all lines from file. I don't need use regular expression(reg exp helps in this case), I need exactly match on second field. Thank you in advance

Upvotes: 1

Views: 263

Answers (1)

Chankey Pathak
Chankey Pathak

Reputation: 21676

Use eq for string comparison instead of == which is used for numeric comparison.

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

Edit: As toolic has suggested in his comment, if you had used warnings, you could have easily spot the issue.

$ 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

Upvotes: 3

Related Questions