patternmatching in file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kriz4321
    New Member
    • Jan 2007
    • 48

    patternmatching in file

    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
    Recognized Expert Specialist
    • Jan 2007
    • 4092

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

    Comment

    • docsnyder
      New Member
      • Dec 2006
      • 88

      #3
      Try this substitution:
      Code:
      $_ =~ s/^(load=).*$/$1$input/;
      Greetz, Doc

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Originally posted by docsnyder
        Try this substitution:
        Greetz, Doc
        Unfortuneately, that will have no affect on the file even though the regexp is correct.

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #5
          you can redirect the output to another file to make the changes
          Code:
          perl yourfile.pl > output
          or define output file handle
          Code:
          ....
           open FILE, "> $filename";
           print $data1 FILE;
          ...

          Comment

          Working...