The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pattern Matching problem in UNIX maxmave Shell Programming and Scripting 2 06-03-2008 02:19 AM
help need for pattern matching HIMANI UNIX for Dummies Questions & Answers 10 01-22-2008 07:30 AM
pattern matching mercuryshipzz Shell Programming and Scripting 4 01-14-2008 11:01 PM
pattern matching in an if-then lumix Shell Programming and Scripting 4 12-14-2007 04:25 PM
Pattern matching sed leemjesse Shell Programming and Scripting 3 03-23-2005 04:06 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-14-2008
maxmave maxmave is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 21
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 (permalink)  
Old 05-14-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 05-14-2008
maxmave maxmave is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 21
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0