-4

i am trying to modify a few strings in a file using perl by using the below logic..

open FILE1, "< /tmp/sam.dsl" //In read mode
open FILE2, "> /tmp/sam2.dsl" // Open in write mode

while(<FILE1>)
if($_=s/string/found/g)
push FILE2, $_...

I am able to change the contents however the when i read the file it has ^M in it..

my datafile is of the below format

name 'SAMPLE'

i would like to change this to

name 'SAMPLE2'

currently with my code it changes to

name 'SAMPLE2
'

which creates a new line and then does the replacement.

Do i need to use anyother mode to open the file to write..?

Tanktalus
  • 21,664
  • 5
  • 41
  • 68
satishkumar432
  • 337
  • 1
  • 6
  • 19
  • currently my code inserts a new line after it reads and then pushes a ^M – satishkumar432 Apr 28 '15 at 22:10
  • 3
    Can you show more of the real code? This pseudo code you wrote there isn't making too much sense. Have you forgotten to `chomp`, have you forgotten the `~`, what are you doing when you have substituted something? Are you using `strict` and `warnings`? If not turn that on. Oh and maybe relevant for that question: what operating system are you working on? – Patrick J. S. Apr 28 '15 at 22:43
  • `code` open TEMPLATEFILE, '<:encoding(UTF-8)', "/tmp/TemplateDSL.dsl" or die "Could not open the file $!"; open JOBFILE, '>:encoding(UTF-8)', "/tmp/sam.dsl" or die " Could not open the file in write mode"; while() { if(/$templateJobName/) { $_=~s/$templateJobName/$projectName/; } if(/$templatePathName/) { #$_=~s/$templatePathName/$pathName/; } print JOBFILE $_; } close TEMPLATEFILE; close JOBFILE; – satishkumar432 Apr 28 '15 at 22:58
  • `code` open TEMPLATEFILE, '<:encoding(UTF-8)', "/tmp/TemplateDSL.dsl" or die "Could not open the file $!"; open JOBFILE, '>:encoding(UTF-8)', "/tmp/sam.dsl" or die " Could not open the file in write mode"; while() { if(/$templateJobName/) { $_=~s/$templateJobName/$projectName/; } if(/$templatePathName/) { #$_=~s/$templatePathName/$pathName/; } print JOBFILE $_; } close TEMPLATEFILE; close JOBFILE; `code` This is the code and the OS is unix..yea.. i am using the strict and warnings... – satishkumar432 Apr 28 '15 at 22:59
  • Update your post with the proper code instead of cramming gibberish into the comments. Make it easier for people to help you. – Sinan Ünür Apr 28 '15 at 23:57
  • im sorry my bad.. let me put it neat/ – satishkumar432 Apr 29 '15 at 00:03
  • As said earlier... `chomp`. – stevesliva Apr 29 '15 at 00:23
  • Thank you ..i did use chomp right before i did a push $FILENAME, $_, but no luck :( – satishkumar432 Apr 29 '15 at 00:27

1 Answers1

0

My guess is, that you are working with a linux file on some windows. Perl automatically converts \n into \r\n on dos-compatible machines after reading and before writing. To get rid of this behaviour, you can use binmode <FILE> on your filehandles, but it sets your filehandle into "raw binary mode". If you want to use some other layers (like :utf8 or :encoding(utf-8)) are not enabled, and you might want to set them yourself, if you are handling character data. You also could use the PerlIO::eol module from CPAN.

Consider looking at these documentation pages:

  • PerlIO for a general understanding how the Perl-IO works.
  • open the pragma (not the function) to set layers for one program.
  • binmode the function you might want to consider.

My suggestion, but I can't test it (no Windows around), would be to use the following:

open my $outfile, '<:encoding(utf-8)', "filename" or die "error opening: $!";
binmode $outfile, join '', grep {$_ ne ':crlf'} PerlIO::get_layers($outfile)
  or die "error setting output:\n $!"

while(<$infile>){
  s/match/replacement/g;
  print $outfile $_;
}
Patrick J. S.
  • 2,885
  • 19
  • 26
  • Thank you Patrick..This is a linux system...and i have crreated this file on the system.. i have tried with utf-8 encoding.. no luck with that.. i need to try with binmode.. – satishkumar432 Apr 29 '15 at 17:01
  • are you sure the file doesn't have `^M`s in the first place? Where do you get your `$projectname` from are you sure it doesn't contain any newlines? You really should edit your question, maybe enough people revoke their downvote and someone else will try to help you. – Patrick J. S. Apr 29 '15 at 18:33
  • ok.. here is the actual implementation i was looking for.. i had to read the last line from the file and then based on the delimiter(;,:..etc) i have to get the 2nd and 3rd fields. Then replace these fields in another file.. You are correct Patrick, apparently the source provided by the other team isnt proper on the server..i checked that with the a dummy substitution... however..i took another approach using shell script by using tail to read the last line and then parsing that line and using echo to push the statements into another script.. I have 2 solutions for the problem now :) Thank U – satishkumar432 Apr 29 '15 at 20:46