search more than one pattern with perl on same line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search more than one pattern with perl on same line
# 1  
Old 02-11-2011
search more than one pattern with perl on same line

Hi friends, I want to search for some hex error codes in some files.
After the hex error code is found, the occurences would be counted.
Afterwards the found hex errorcode would be cat into a separate file.

Here is my code:

Code:
 
#!/usr/bin/perl
use File::Basename;
my $find = $ARGV[0];
my @filelist = $ARGV[1];
my($name,$dir,$ext) = fileparse($ARGV[1],'\..*');
my $newfilename = ($name . "_" . $find . ".csv");
my $intvent = $ARGV[2];
my $workflowid = $ARGV[4];
##example for my $system1: 
#cat /var/mdex/State/ds_state_$workflowid.csv | grep "\0x0003" > /tmp/KGW_$workflowid/ds_state_$workflowid_0x0003.csv
my $system1="cat". " " . $ARGV[1] . " " . "| grep " . " " .  "\"" . "\\" . $ARGV[0] . "\"" . " > " . "/tmp/KGW_" .  $workflowid ."\/" . 
$newfilename;
# process each file in file list
foreach $filename (@filelist) {
      $count = 0;
      # retrieve complete file
      open (IN, "$filename") || die("Error Reading File: $filename $!");
      {
        undef $/;
        $infile = <IN>;
        while ($infile =~ m/$find/gi)
            {$count++};
        print (($count == 0  ) ? exit(0)  : $count . "* " . "\t" . $find . " " . $intvent);
        if ($count >= 0) {
        # write the found ds or up errorcodes into a separate csv file
        system ($system1);
        }
       }
       close (IN) || die("Error Closing File: $filename $!");
}
print "\n";


The /tmp/ds_state_1222.csv looks like this one:

Code:
ADEA576B3030303030303030;459;SAM_LISR;SIMPLIFIED;549;35789;0;0;0x0003 [AUDA900052649];0x0002 [AUDA900052649];
BB4604963030303030303030;411;SAM_LISR;CAPITALIZED;553;45553;0;0;0x0003 [AUDA900056819];0x0002 [AUDA900056819];
26E613683030303030303030;40;SAM_LISR;COPY;553;45709;0;0;0x0003 [AUDA900056830];0x0002 [AUDA900056830];


./check_mdex_codes.pl 0x0003 /tmp/ds_state_1222.csv "(no intervention needed)" echo "$1"

As a result the following would be printed with this perl script:
3* 0x0003 (no intervention needed)

Now I want to search for more than one pattern witin this perl script:

1. I want to search for 0x0003 and SIMPLIFIED occurence on the same line.
./check_mdex_codes.pl 0x0003 SIMPLIFIED /tmp/ds_state_1222.csv "(no intervention needed)" echo "$1"


2. I want to search for 0x0003 and CAPITALIZED occurence on the same line.
./check_mdex_codes.pl 0x0003 CAPITALIZED /tmp/ds_state_1222.csv "(no intervention needed)" echo "$1"


3. I want to search for 0x0003 and COPY occurence on the same line.
./check_mdex_codes.pl 0x0003 COPY /tmp/ds_state_1222.csv "(no intervention needed)" echo "$1"

So that as a result I would get:
1* 0x0003 SIMPLIFIED (no intervention needed)
1* 0x0003 CAPITALIZED (no intervention needed)
1* 0x0003 COPY (no intervention needed)


How to search for more than one search pattern on the same line with perl?

regards
sdohn
# 2  
Old 02-11-2011
Here is one way of doing it
Code:
$ cat infile.txt
123 abc defDOERAEMEaea
fsfsfsf sdsdfsa

$ cat example.pl
#!/usr/local/bin/perl

use strict;
use warnings;

my $file  = "infile.txt";
my $pattern1 = "123";
my $pattern2 = "abc";
my $pattern3 = "DOERAEME";

open (IN, "< $file") || die "ERROR: opening $file\n";

while (<IN>)
{
    if (/$pattern1/ && /$pattern2/ && /$pattern3/) {
       print "found all patterns\n";
    }
    if (/$pattern1/ || /$pattern2/ || /$pattern3/) {
       print "found one or more patterns\n";
    }
}

close IN;

$ ./example
found all patterns
found one or more patterns
$

This User Gave Thanks to fpmurphy For This Post:
# 3  
Old 02-14-2011
Thanks a lot to you!
That code did it!

with kindest regards
From
Stefan M. Dohn
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search a pattern in a line and remove another pattern

Hi, I want to search a pattern in a text file and remove another pattern in that file. my text file look like this 0.000000 1.970000 F 303 - 1.970000 2.080000 VH VH + 2.080000 2.250000 VH VH + 2.250000 2.330000 VH L - 2.330000 2.360000 F H + 2.360000 2.410000 L VL - 2.410000 ... (6 Replies)
Discussion started by: sreejithalokkan
6 Replies

2. Shell Programming and Scripting

How to search pattern and add that pattern in next line

Hi All, I am new to shell scripting and need help in scripting using CSH. Here is what I am trying to so, 1. Search a specific string e.g. "task" from "task (input1, out1)". 2. Extract the arguements "input1" and "out1" 3. Add them in separate lines below. eg. "int input1" , " integer out1" ... (7 Replies)
Discussion started by: deshiashish
7 Replies

3. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

4. Shell Programming and Scripting

perl pattern search

can someone help me out with the bolded? else if (regmatch($Subject, "^Application") && (regmatch($From, "^etgh") && (regmatch($Body, ".*not authorized to use this server.*")))) what this section of the code is suppose to do is to scan through the contents of $Body, if do a set of... (1 Reply)
Discussion started by: SkySmart
1 Replies

5. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

6. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

7. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies

8. Shell Programming and Scripting

Perl Search pattern error

Hi, I am trying to grep for two patterns from a set of 820 apache webserver logs, When I Try to search the pattern with a date stamp and use a wildcard character * for ex: /28/Aug/2008:21*/ to get all the log entries for that particular hour that is 21 st hour I get errors. Please kindly... (1 Reply)
Discussion started by: openspark
1 Replies

9. Shell Programming and Scripting

Perl onliner to search the last line with an occurence of a pattern

Hi I need a perl onliner which seaches a line starting with a pattern(last occurence) and display it. similar to grep 'pattern' filename | tail -1 in UNIX Ex: I want to display the line starting with "cool" and which is a last occurence adadfadafadf adfadadf cool dfadfadfadfara... (4 Replies)
Discussion started by: ammu
4 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question