Perl:how to find multiple strings


 
Thread Tools Search this Thread
Top Forums Programming Perl:how to find multiple strings
# 1  
Old 04-07-2011
Perl:how to find multiple strings

Hi , i'm trying to find lines in a file that have 2 matches,but i have trouble with regex -it doesn't search for a second string-please help
Code:
#!/usr/bin/perl -w
use strict;
use warnings;

my $log = "log";
my $log_string = "grep '^8=' $log |";
usage ();
chomp (my $TAB_1 = $ARGV[0]);
chomp (my $TAB_5 = $ARGV[1]); 
open (my $HAN, "$log_string") || die "Problems grepin... : $!";
my @contents = <$HAN>;
my $matches = grep /1=$TAB_1 && 5=$TAB_5/, @contents ;
 foreach (@matches) 
   { 
      print "[+] FOUND $TAB_1 and $TAB_5 in\n",
"-----------------------\n",
"$_" ;}

now you can do it like
Code:
foreach (@contents)
if /1=$TAB_1 && 5=$TAB_5 /

that also doesn't find lines with those 2 matches

Last edited by radoulov; 04-08-2011 at 04:36 PM.. Reason: Code tags!
# 2  
Old 04-09-2011
Hi.

The code you posted had translation errors. I wrote a driver script to run a corrected version:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate driver for perl code.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C perl

FILE=${1-data1}
pl " Data file $FILE:"
cat $FILE

pl " Results of ./p1 a z $FILE:"
./p1 a z $FILE

exit 0

calling the perl code:
Code:
#!/usr/bin/perl -w
use strict;
use warnings;

# my $log = "log";
# my $log_string = "grep '^8=' $log |";
# usage ();
chomp (my $TAB_1 = $ARGV[0]);
chomp (my $TAB_5 = $ARGV[1]); 
shift ; shift ;
# open (my $HAN, "$log_string") || die "Problems grepin... : $!";
# my @contents = <$HAN>;
# my $matches = grep /1=$TAB_1 && 5=$TAB_5/, @contents ;
my @contents = <>;
# my @matches = grep /1=$TAB_1 && 5=$TAB_5/, @contents ;
my @matches = grep /$TAB_1/ && /$TAB_5/, @contents ;
 foreach (@matches) 
   { 
      print "[+] FOUND $TAB_1 and $TAB_5 in\n",
"-----------------------\n",
"$_" ;}

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
GNU bash 3.2.39
perl 5.10.0

-----
 Data file data1:
a
b
a b
x
a z
x y
z

-----
 Results of ./p1 a z data1:
[+] FOUND a and z in
-----------------------
a z

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 3  
Old 04-11-2011
Well Done -Nice ONE!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. Shell Programming and Scripting

Perl script to find process and exclude strings from the output

Hi team, I'm a newbie of Perl Script and looking to create a simple perl script that will run in the Linux system: 1) to find process, such as ps -ef | grep process name 2) to exclude strings from the output if it found, for instance if i see abc from usr process, then will exclude it from... (1 Reply)
Discussion started by: hoffman2503
1 Replies

3. Solaris

How to find multiple strings on different lines in file?

Hello, I have spent considerable amount of time breaking my head on this and reached out here. here is the back ground. OS - Solaris 10 There are two strings '<Orin>sop' and '<Dup>two' which I wanted to look for in a file without the quotes on different lines and ONLY if both strings are... (5 Replies)
Discussion started by: keithTait309875
5 Replies

4. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 Replies

5. Shell Programming and Scripting

How to find multiple strings within files under a directory

Hi guys, I need to find multiple strings within files under a directory and secondly, to count how many files are there with these strings. At present, i am able to do this in order to find one string (for example "abc"): find <path> -exec grep "abc" {} /dev/null \; Now for example, i... (2 Replies)
Discussion started by: frum
2 Replies

6. Shell Programming and Scripting

Find position of character in multiple strings in a file

Greetings. I have a file with information like this: AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU? AMNDHRKEEU?AMNDHREOEU? AMNDHREU?AHRKEOEU?AMNDHRKEU?AMNDKEOEU? What I need to extract is the position, in every line, of every occurrence of '?' A desired output would be something... (6 Replies)
Discussion started by: Twinklefingers
6 Replies

7. UNIX for Dummies Questions & Answers

Find Multiple Strings from a list of *.gz files withour decompressing...

Hello Team, There is this situation where there are around 20 *.gz files and i want to search multiple words from all those files. Example as below : filea.gz fileb.gz filec.gz now i want to search words "hi" and "hello" from all these 3 files without... (4 Replies)
Discussion started by: varun87
4 Replies

8. UNIX for Dummies Questions & Answers

how to find and replace strings in multiple files

Hi All, Iam new to unix, I need to find string and replace it in the file name. Like text_123_0.txt,text_123_1.txt,text_123_2.txt. I need to search 123 and replace it with 234 . Is there any unix command to replace them in single command since i have 5 directories. So i need to go each and every... (0 Replies)
Discussion started by: etldeveloper
0 Replies

9. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

10. Linux

To find multiple strings count in a file

I need to find the line count of multiple strings in a particular file. The strings are as follows: bmgcc bmgccftp bsmsftp bulkftp cctuneftp crbtftp crmpos cso gujhr I am doing manual grep for each of the string to find the line count. The command i am using right now is: grep mark... (3 Replies)
Discussion started by: salaathi
3 Replies
Login or Register to Ask a Question