[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to use grep for multiple strings in perl

Posted on 2009-02-16
4
Medium Priority
?
1,271 Views
Last Modified: 2012-06-27
Using perl I need to ssh to a remote host and grep a file for strings.  For example, if I want to search just 'string1'  I would do the line of code below and it works.

$my result = qx{ssh $host grep string1 $log_file};

however I want to search 2 or 3 strings in the file. I tried
$my result = qx{ssh $host grep 'string1\|string2' $log_file};

but that does not work.  
The grep command is right because if execute from the shell prompt I get the lines containing either string1 or string2.
0
Comment
Question by:urivera
  • 2
4 Comments
 
LVL 3

Assisted Solution

by:ht-docs
ht-docs earned 200 total points
ID: 23653855
Try this:

open(PIPE, "ssh $host grep string1 $log_file |");
my @output = <PIPE>;

@output should contain all strings returned by the command
0
 
LVL 3

Expert Comment

by:ht-docs
ID: 23653867
Oh, in your case command inside 'open' should like as "ssh $host grep 'string1\|string2' $log_file" of course
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 400 total points
ID: 23654046
my $result = qx{ssh $host grep -e string1 -e string2 $log_file};
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 23654060
qx{ssh $host grep 'string1\\\\\\|string2' $log_file}
or
grep /string1|string2/ qx{ssh $host cat $log_file}
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question