prasadbhagwat
prasadbhagwat

Reputation: 91

How to remove ^M character from file which is generated on Fly using Perl Script

I am creating normal txt file on the basis of provided configuration so when I am trying to remove Control M from file it is giving me error as

syntax error at CreateConfig.pl line 141`, near `perl -p -i -e "s/\r\n/\n/g`
Execution of `CreateConfig.pl` aborted due to compilation errors.

Code line:

system("perl -p -i -e "s/\r\n/\n/g" OptionFile.txt");

Can anyone has any suggestion for same?

Upvotes: 0

Views: 872

Answers (2)

choroba
choroba

Reputation: 241998

You can't nest double quotes inside doublequote. Use single quotes instead one of them.

If you're calling it from Perl, use single quotes for the outer ones, otherwise Perl will interpolate the \r and \n. Or, replace it with code that does the same directly without calling Perl again. The best place to remove them would be the same where you read the file.

Upvotes: 2

Borodin
Borodin

Reputation: 126742

Presumably OptionFile.txt has been created on a Windows system? Rather than using a system call to edit the file in place before opening it, it is probably better to handle this when you read from the file

If you write

open my $fh, '<:crlf', 'OptionFile.txt' or die $!;

then the CR (control-M) characters will be removed by Perl on input

Alternatively, if you already have a chomp in your read loop then you can replace that with s/\R\z//. \R matches any standard line ending, and so will strip the trailing CR LF from a Windows file

Upvotes: 3

Related Questions