Pattern Matching in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern Matching in UNIX
# 1  
Old 05-14-2008
Help !! Pattern Matching in PERL

Hello,

I have a pattern like "XXXXXX XXXXXX" which i need to make search in a input file and Replace the matched pattern to a another pattern.

This is the code i tried ..
#!/usr/bin/perl
print "Enter a File name :";
chomp ($file = <STDIN>);
print "\n Searching file :";
if (system ("ls $file")==0)
{
print "File Found\n";

$lines = `wc -l $file`;
@personal = split(/ /, $lines);
$lines = @personal[0];

print "Total number of lines in the file = $lines \n";

print "Enter the pattern to search :";
chomp ($pattern = <STDIN>);
print "\n";
# to search the no of words (pattern search)
$abc=`grep $pattern $file | wc -l`;
print "Total number of results found $abc \n";
print "here are the results ...\n";
system("grep $pattern $file");
}
else{
print "File not Found\n";
}
This code works where i input a pattern without spaces like "MAX",
But when i give a Pattern like this "MAX XAM", the script errors out.

I tried to put "/s" also, its erroring out.

How can i search a pattern which is embeded with a space between two words.

Can any one please help.

Thanks

Rahul

Last edited by maxmave; 05-14-2008 at 04:04 PM..
# 2  
Old 05-14-2008
You need to properly quote variables with spaces or other special characters in them.

If you want to use Perl, it's rather weird to not do the actual matching in Perl also. Running the grep twice just because you want the number of matches first is also ... intriguing. Here, I cannot resist.

Code:
#!/usr/bin/perl
print "Enter a File name :";
chomp ($file = <STDIN>);
print "\n Searching file :";
if (-e $file)
{
    print "File Found\n";

    $lines = `wc -l < $file`;
    chomp $lines;

    print "Total number of lines in the file = $lines \n";

    print "Enter the pattern to search :";
    chomp ($pattern = <STDIN>);
    print "\n";
    # to search the no of words (pattern search)
    $abc=`grep "$pattern" $file`;
    $count = () = $abc =~ m/\n/g;
    print "Total number of results found: $count\n";
    print "here are the results ...\n$abc\n";
}
else{
    print "File not Found\n";
}

# 3  
Old 05-14-2008
Hello Era,

Thanks for the corrections, it really worked.

I am just beginner to scripting, that why iam rusty.

but your suggestions really helped me, will correct the mistakes.

Thanks

Rahul
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX command to get the latest file and also matching pattern

we want to fetch the latest file in a given directory and also the file name should match the below pattern Example file name ->hrdata-2015-10-13-16-45-26.xml(2015-10-13-16-45-26- it is not current timestamp, we just need to check for the pattern) We expect the file will have the... (3 Replies)
Discussion started by: vishwanath001
3 Replies

2. UNIX for Beginners Questions & Answers

Binary pattern matching in UNIX

I think what I'm trying to do is pretty straightforward but I just can't find a way to do it. I'm trying to run a double pattern match in a three column file. If the first two columns match, I need to output the third. So in the file AAA BBB 1 BBC CCC 5 CCC DDD 7 DDD EEE 12 If the... (4 Replies)
Discussion started by: matthewndavies1
4 Replies

3. Shell Programming and Scripting

UNIX awk pattern matching and printing lines

I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well. cat result.txt Page 2015-01-01 2000 Colors 2015-02-01 3000 Landing 2015-03-02 4000 #!/bin/sh LOG=/tmp/maillog.txt... (1 Reply)
Discussion started by: close2jay
1 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. Shell Programming and Scripting

pattern matching on any special character in Unix

Hi, I have field in a file which would come with any special character, how do i check that field? Eg: @123TYtaasa>>>/ 131dfetr_~2 In the above example, how do I add pattern for any special character on the keyboard. Thanks (3 Replies)
Discussion started by: techmoris
3 Replies

6. Shell Programming and Scripting

Issue with Pattern Matching in Unix

Hi, I am trying to replace a specific column values in a csv file with double quotes. Example: SNO,NAME,ZIPCODE,RANK,CARE_OF 1,Robert,74538,12,RICHARD JOHNSON, P.C 2,Sam,07564,13,% R.S MIKE, V.K.S 3,Kim, Ed,12345,14,@90 KMS, %TK Desired Output: SNO,NAME,ZIPCODE,RANK,CARE_OF... (1 Reply)
Discussion started by: techmoris
1 Replies

7. Shell Programming and Scripting

Perl Pattern Matching:Unix Vs. Window (Is it OS dependent??)

Hi All, Below I am discussing the problem I am facing while using pattern matching in Unix & Window. Plz hv a look into it. ================ my $s="UPDATE A SET s="klkkk' ;" ; if ( $s =~ m/^*UPDATE+/i ) { print $s; } else { print "no match";} =================== Both should... (6 Replies)
Discussion started by: Niroj
6 Replies

8. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 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: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

9. UNIX for Advanced & Expert Users

pattern matching in unix

Task is to identify files like code.1 , code.23 and so on ... (the files which are ending with a number) but it should not match files like code.123abc. So the search will normally search for files with "code." and at the end we should extract for the correct match. Now I have to remove these files... (6 Replies)
Discussion started by: diwakar_reddy
6 Replies

10. Shell Programming and Scripting

Pattern Matching problem in UNIX

Hello All, I need help I have a problem in searching the pattern in a file let us say the file contains the below lines line 1 USING *'/FILE/FOLDER/RETURN') ................. ................. line 4 USING *'/FILE/FOLDER/6kdat1') line 5 USING... (2 Replies)
Discussion started by: maxmave
2 Replies
Login or Register to Ask a Question