![]() |
|
|
|
|
|||||||
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. Shell Script Page. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pattern Matching problem in UNIX | maxmave | Shell Programming and Scripting | 2 | 06-02-2008 10:19 PM |
| help need for pattern matching | HIMANI | UNIX for Dummies Questions & Answers | 10 | 01-22-2008 03:30 AM |
| pattern matching | mercuryshipzz | Shell Programming and Scripting | 4 | 01-14-2008 07:01 PM |
| pattern matching in an if-then | lumix | Shell Programming and Scripting | 4 | 12-14-2007 12:25 PM |
| Pattern matching sed | leemjesse | Shell Programming and Scripting | 3 | 03-23-2005 12:06 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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/perlThis 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. |
| Forum Sponsor | ||
|
|
|
|||
|
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";
}
|
| Thread Tools | |
| Display Modes | |
|
|