LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-15-2011, 02:37 AM   #1
ckchelladurai
LQ Newbie
 
Registered: Jun 2011
Location: chennai
Posts: 2

Rep: Reputation: Disabled
Angry how to use find and replce command in perl script


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....

Thanks,
Chella...
 
Old 06-15-2011, 03:25 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Code:
system("perl -pi -e 's/\r\n/\n/g' *.cfg");
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
 
Old 06-15-2011, 07:21 AM   #3
ckchelladurai
LQ Newbie
 
Registered: Jun 2011
Location: chennai
Posts: 2

Original Poster
Rep: Reputation: Disabled
Hi,
Thanks for your reply. Will i use this perl code in end of my existing perl code???
I am very new to the perl script.. :-(
 
Old 06-16-2011, 10:26 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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)
 
Old 06-16-2011, 08:53 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Alternately, there's always the dos2unix util.
http://linux.die.net/man/1/dos2unix
 
Old 06-17-2011, 04:41 AM   #6
Noway2
Senior Member
 
Registered: Jul 2007
Distribution: Gentoo
Posts: 2,125

Rep: Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781
Quote:
system("perl -pi -e 's/^M//g' *.cfg");
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.
Code:
system("perl -pi -e 's/\cM//g' *.cfg");
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find out if X11 is running from within perl OR bash script smeezekitty Programming 21 09-10-2011 05:22 PM
Find and replace a string in a file using perl command from bash script koundinya749 Programming 5 02-15-2011 04:52 PM
Use of "Command line perl" in perl script using system command. aditya007 Linux - Newbie 4 11-29-2009 10:08 PM
Help me find a Perl script rumblestrut Linux - Server 1 05-25-2007 09:46 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration