Sponsored Content
Top Forums Programming Perl:how to find multiple strings Post 302511774 by auto_w on Thursday 7th of April 2011 01:47:48 PM
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!
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Mail::Box::Search::Grep(3pm)				User Contributed Perl Documentation			      Mail::Box::Search::Grep(3pm)

NAME
Mail::Box::Search::Grep - select messages within a mail box like grep does INHERITANCE
Mail::Box::Search::Grep is a Mail::Box::Search is a Mail::Reporter SYNOPSIS
use Mail::Box::Manager; my $mgr = Mail::Box::Manager->new; my $folder = $mgr->open('Inbox'); my $filter = Mail::Box::Search::Grep->new ( label => 'selected' , in => 'BODY', match => qr/abc?d*e/ ); my @msgs = $filter->search($folder); my $filter = Mail::Box::Search::Grep->new ( field => 'To' , match => $my_email ); if($filter->search($message)) {...} DESCRIPTION
Try to find some text strings in the header and footer of messages. Various ways to limit the search to certain header fields, the whole header, only the body, the whole message, but even binary multiparts, are provided for. The name grep is derived from the UNIX tool grep, which means: "Get Regular Expression and Print". Although you can search using regular expressions (the Perl way of them), you do not have to print those as result. METHODS
Constructors Mail::Box::Search::Grep->new(OPTIONS) Create a UNIX-grep like search filter. -Option --Defined in --Default binaries Mail::Box::Search <false> decode Mail::Box::Search <true> delayed Mail::Box::Search <true> deleted Mail::Box::Search <false> deliver undef field undef in Mail::Box::Search <$field ? 'HEAD' : C<'BODY'>> label Mail::Box::Search undef limit Mail::Box::Search 0 log Mail::Reporter 'WARNINGS' logical Mail::Box::Search 'REPLACE' match <required> multiparts Mail::Box::Search <true> trace Mail::Reporter 'WARNINGS' binaries => BOOLEAN decode => BOOLEAN delayed => BOOLEAN deleted => BOOLEAN deliver => undef|CODE|'DELETE'|LABEL|'PRINT'|REF-ARRAY Store the details about where the match was found. The search may take much longer when this feature is enabled. When an ARRAY is specified it will contain a list of references to hashes. Each hash contains the information of one match. A match in a header line will result in a line with fields "message", "part", and "field", where the field is a Mail::Message::Field object. When the match is in the body the hash will contain a "message", "part", "linenr", and "line". In case of a CODE reference, that routine is called for each match. The first argument is this search object and the second a reference to same hash as would be stored in the array. The "PRINT" will call printMatchedHead() or printMatchedBody() when any matching header resp body line was found. The output is minimized by not reprinting the message info on multiple matches in the same message. "DELETE" will flag the message to be deleted in case of a match. When a multipart's part is matched, the whole message will be flagged for deletion. field => undef|STRING|REGEX|CODE Not valid in combination with "in" set to "BODY". The STRING is one full field name (case-insensitive). Use a REGEX to select more than one header line to be scanned. CODE is a routine which is called for each field in the header. The CODE is called with the header as first, and the field as second argument. If the CODE returns true, the message is selected. in => 'HEAD'|'BODY'|'MESSAGE' label => STRING limit => NUMBER log => LEVEL logical => 'REPLACE'|'AND'|'OR'|'NOT'|'AND NOT'|'OR NOT' match => STRING|REGEX|CODE The pattern to be search for can be a REGular EXpression, or a STRING. In both cases, the match succeeds if it is found anywhere within the selected fields. With a CODE reference, that function will be called each field or body-line. When the result is true, the details are delivered. The call formats are $code->($head, $field); # for HEAD searches $code->($body, $linenr, $line); # for BODY searches The $head resp $body are one message's head resp. body object. The $field is a header line which matches. The $line and $linenr tell the matching line in the body. Be warned that when you search in "MESSAGE" the code must accept both formats. multiparts => BOOLEAN trace => LEVEL Searching $obj->inBody(PART, BODY) See "Searching" in Mail::Box::Search $obj->inHead(PART, HEAD) See "Searching" in Mail::Box::Search $obj->search(FOLDER|THREAD|MESSAGE|ARRAY-OF-MESSAGES) See "Searching" in Mail::Box::Search $obj->searchPart(PART) See "Searching" in Mail::Box::Search The Results $obj->printMatch([FILEHANDLE], MATCH) $obj->printMatchedBody(FILEHANDLE, MATCH) $obj->printMatchedHead(FILEHANDLE, MATCH) Error handling $obj->AUTOLOAD() See "Error handling" in Mail::Reporter $obj->addReport(OBJECT) See "Error handling" in Mail::Reporter $obj->defaultTrace([LEVEL]|[LOGLEVEL, TRACELEVEL]|[LEVEL, CALLBACK]) Mail::Box::Search::Grep->defaultTrace([LEVEL]|[LOGLEVEL, TRACELEVEL]|[LEVEL, CALLBACK]) See "Error handling" in Mail::Reporter $obj->errors() See "Error handling" in Mail::Reporter $obj->log([LEVEL [,STRINGS]]) Mail::Box::Search::Grep->log([LEVEL [,STRINGS]]) See "Error handling" in Mail::Reporter $obj->logPriority(LEVEL) Mail::Box::Search::Grep->logPriority(LEVEL) See "Error handling" in Mail::Reporter $obj->logSettings() See "Error handling" in Mail::Reporter $obj->notImplemented() See "Error handling" in Mail::Reporter $obj->report([LEVEL]) See "Error handling" in Mail::Reporter $obj->reportAll([LEVEL]) See "Error handling" in Mail::Reporter $obj->trace([LEVEL]) See "Error handling" in Mail::Reporter $obj->warnings() See "Error handling" in Mail::Reporter Cleanup $obj->DESTROY() See "Cleanup" in Mail::Reporter $obj->inGlobalDestruction() See "Cleanup" in Mail::Reporter DIAGNOSTICS
Error: Package $package does not implement $method. Fatal error: the specific package (or one of its superclasses) does not implement this method where it should. This message means that some other related classes do implement this method however the class at hand does not. Probably you should investigate this and probably inform the author of the package. SEE ALSO
This module is part of Mail-Box distribution version 2.105, built on May 07, 2012. Website: http://perl.overmeer.net/mailbox/ LICENSE
Copyrights 2001-2012 by [Mark Overmeer]. For other contributors see ChangeLog. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html perl v5.14.2 2012-05-07 Mail::Box::Search::Grep(3pm)
All times are GMT -4. The time now is 05:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy