Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, I have one perl sctipt which generate cfg file with ^M in all the EOL. I need to remove the ^M from the cfg file. How can i remove that from the perl script itself.
I have tried the following unix command in perl script,
system("perl -pi -e 's/^M//g' *.cfg");
It doesn't work.
Could you please help on this....
A "better" (read as 'without using the system() call') solution would be:
Code:
@files = <*.cfg>;
foreach $filename (@files) {
if(-f $filename && -r $filename && -w $filename){
rename("$filename","$filename.bak");
open INPUT, "$filename.bak";
open OUTPUT, ">$filename";
while( <INPUT> ) {
s/\r\n$/\n/;
print OUTPUT $_;
}
close INPUT;
close OUTPUT;
unlink("$filename.bak");
}
}
It's creating it in DOS format, sounds like. Though an even better solution would be to change the first script, and make sure it only outputs the '\n', not the '\r' as well.
Last edited by Snark1994; 06-15-2011 at 03:31 AM.
Reason: Added pure-perl solution
It should work if you put it where you had the "system()" line. You may have to use 'my @files;' and 'my $filename;' to avoid errors.
However, I only really posted the script to show that you don't need the system call. Like I said, the best way to fix it is to read through the first perl script (which creates the .cfg files), and change where it tries to print '\r\n' to only printing '\n'
Note: If you're running Windows (which your OS icon suggests) then using '\r\n' as a line ending is the default. In which case, perhaps you could use a different editor that supports both Unix and DOS line endings (such as vim)
I have used perl to remove the ^M (control-M) characters at the end of the line. Sed would work too, as might grep with the output passed to another file.
I think the problem is in your regular expression. In regular expressions, ^ is start of the line, not the control key. This means your script is trying to find lines that start with the letter M and erase the M character. Not quite what you want.
I believe what you want is to use the \cM where the \c is an escape sequence representing the control key. Try this instead.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.