AnnaSchumann
AnnaSchumann

Reputation: 1271

Print line if multiple matches

I'm very new to Perl and trying to get this to work. I wish to print 3 specific columns when the contents of column 1 == >0 and column 2 contains "I" (the roman numeral for 1). The following returns all lines containing >0 but NOT containing "I".

perl -lane 'print "$F[0]\t$F[1]\t$F[5]" if $F[1] > 0 && $F[0]==/I/' input > output

What have I done wrong? I'm also struggling to get it to match the pattern exactly i.e. I do not want it to pull out "II" or "VIII" for example.

Upvotes: 1

Views: 67

Answers (1)

Borodin
Borodin

Reputation: 126762

You need to use the binding operator =~, not == which is a numeric equality test, to test a string against a regex pattern

And you need to anchor the start and end of the pattern if you want to specify its entire contents. Read about it in Metacharacters

$F[0]==/I/

should be

$F[0] =~ /^I$/

or, better, just use a string equality test

$F[0] eq 'I'

Upvotes: 5

Related Questions