Loading ...
Sorry, an error occurred while loading the content.

Rename inside a file

Expand Messages
  • Toma H
    Hello I start to use perl and can be fun and also can help a lot but I do not know who to make it to work have another problem I am trying to rename a string
    Message 1 of 2 , Nov 4, 2010
    • 0 Attachment
      Hello I start to use perl and can be fun and also can help a lot but I
      do not know who to make it to work have another problem

      I am trying to rename a string in a file with another string I have list
      of strings in remane.txt and input file accountin.txt I try to do this
      but somewere I do not know what is the problem
      I have use also http://codepad.org/WlJKat0K
      <http://codepad.org/WlJKat0K> but no results if someone can show me
      were is my misstake please advice me

      [code]
      use warnings;
      #input data
      my $filename = 'accountin.txt';
      my $renamedata = 'rename.txt';
      #load rename list
      local @var = ($renamedata);
      $var = $_;
      {
      local @ARG = ($renamedata);
      #if data in rename file then give value to old and new value in the
      accoutin file
      while ( $renamedata ) {
      my ($find,$replace) = split /;/, $;

      #if data in account data then search for value find and replace
      with$replace
      local @ARGV = ($filename);
      while( <> )
      {
      if( s/$find/$replace/ig ) {
      print;
      }
      else {
      print;
      }
      }
      }
      }
      [/code] }


      rename.txt
      cart1;cart 1 city zip
      cart2;cart 2 city zip
      cart3;cart 3 city zip

      accountin.txt
      cart1|address|lastname|fname
      cart1|address|lastname|fname
      cart2|address|lastname|fname
      cart1|address|lastname|fname
      cart3|address|lastname|fname
      cart1|address|lastname|fname
      cart1|address|lastname|fname
      cart2|address|lastname|fname
      cart1|address|lastname|fname
      cart2|address|lastname|fname
      cart2|address|lastname|fname




      [Non-text portions of this message have been removed]
    • twlewis64
      Try this: #!/usr/local/bin/perl use strict; my ($elementIn,$renameLine); #input data my $filename = accountin.txt ; my $renamedata = rename.txt ; #load
      Message 2 of 2 , Nov 9, 2010
      • 0 Attachment
        Try this:

        #!/usr/local/bin/perl
        use strict;
        my ($elementIn,$renameLine);
        #input data
        my $filename = 'accountin.txt';
        my $renamedata = 'rename.txt';
        #load rename list

        open(RENAMEDATAFILE,$renamedata); #open the file with the rename data
        my @renameData=<RENAMEDATAFILE>; #load it into an array
        close(RENAMEDATAFILE); #close the file
        my $rdCount = @renameData;

        open(INPUTFILE,$filename); #open the input file
        my @inputFile=<INPUTFILE>; #load the data into an array
        close(INPUTFILE); #close the file
        my $inputFileCount=@inputFile; # get a count of the number of items in the array
        foreach $elementIn(@inputFile) { # loop through the data in
        for (my $count=0;$count<$rdCount;$count++) { #loop through the replacement data
        $renameLine = @renameData[$count];
        my @renameFields = split(/\;/, $renameLine);
        my $oldItem = @renameFields[0];
        my $newItem = @renameFields[1];
        $newItem=~s/\x{0A}//g; # replace ASCII 10 line feed with nothing
        $newItem=~s/\x{0D}//g; # replace ASCII 13 carriage return with nothing
        $elementIn =~ s/$oldItem/$newItem/g; # replace the old data with new data
        }
        }

        my $outputFile = 'c:\temp\accountin_new.txt';
        open(OUTPUTFILE, ">$outputFile");
        foreach $elementIn(@inputFile) { # loop through the data in
        print OUTPUTFILE "$elementIn";
        }
      Your message has been successfully submitted and would be delivered to recipients shortly.