user3349964
user3349964

Reputation: 3

Perl regularexpression match if found print whole line

I have many lines

  Hi i need to work for
  This place
  Which adds me
  More value in this
   wolrd
  Where i can explore my technical skills

i am searching for the words adds me =~ /adds/

If the match found i have to print the whole line "Which adds me"

How to do this in perl

Upvotes: 0

Views: 177

Answers (6)

Ronin Tom
Ronin Tom

Reputation: 188

I have wondered if one can solve the problem using the option -p. Finally I have found this oneliner

perl -pe 'goto LINE unless /adds/;' filename

Upvotes: 0

Jeef
Jeef

Reputation: 27265

while (<FILE>) {

   if (/adds/) { print $_;}
}

Upvotes: 0

user1244533
user1244533

Reputation: 101

if its in file

open(FH,'filename");

while(<FH>){

    if($_=~/adds/){
        print $_;
    }
}

Upvotes: 0

vahid abdi
vahid abdi

Reputation: 10278

With perl command:

perl -ne '/adds/ && print $_' file

output:

  Which adds me

in script.pl:

while (<>) {
    print if (/adds/);
}

then invoke script:

perl script.pl file

Upvotes: 1

Naghaveer R
Naghaveer R

Reputation: 2944

while (<STDIN>) {
  if($_ =~ m/adds me/) { ##this worked for me
    print $_;}
}

Upvotes: 1

Vijay
Vijay

Reputation: 67221

perl -lne 'print if(/\badds\b/)' your_file

tested here

Upvotes: 2

Related Questions