Connecting Tech Pros Worldwide Help | Site Map

patternmatching in file

Member
 
Join Date: Jan 2007
Posts: 48
#1: Jan 25 '07
Hi all

code::
open (FH1, "list.txt") || die "Can't find the data file";
print "Enter the name you need";
$input = <STDIN>;
while(<FH1>)
{
$x =0;
$data1 = $_;
if($_ =~/load/)
{
$x = 1;
print $data1;
}
}
if($x)
{
print "No match found ";
}
close(FH1);



if the contents of the list file is
load=ram
load=sam
when I run my perl script it should ask for a input if I give the Input as
" test" then the contents of the file should be changed as

load=test
load=test

can anyone tell me how to do this,I have given the code I did so for..
Thnks in advance
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Jan 25 '07

re: patternmatching in file


post your code and please use the code tags. Also see this thread:

http://www.thescripts.com/forum/thread591710.html
docsnyder's Avatar
Member
 
Join Date: Dec 2006
Location: Darmstadt
Posts: 88
#3: Jan 26 '07

re: patternmatching in file


Try this substitution:
Expand|Select|Wrap|Line Numbers
  1. $_ =~ s/^(load=).*$/$1$input/;
Greetz, Doc
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Jan 26 '07

re: patternmatching in file


Quote:

Originally Posted by docsnyder

Try this substitution:
Greetz, Doc

Unfortuneately, that will have no affect on the file even though the regexp is correct.
Expert
 
Join Date: Apr 2006
Posts: 512
#5: Jan 27 '07

re: patternmatching in file


you can redirect the output to another file to make the changes
Expand|Select|Wrap|Line Numbers
  1. perl yourfile.pl > output
  2.  
or define output file handle
Expand|Select|Wrap|Line Numbers
  1. ....
  2.  open FILE, "> $filename";
  3.  print $data1 FILE;
  4. ...
  5.  
Reply


Similar Perl bytes