The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 05-14-2008
maxmave maxmave is offline
Registered User
 

Join Date: Apr 2008
Posts: 20
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 12:04 PM.
Reply With Quote
Forum Sponsor