Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > how to match '\r\n' in dos environment

Reply
Thread Tools

how to match '\r\n' in dos environment

 
 
Liang
Guest
Posts: n/a
 
      08-27-2004
hi,

I want to convert a file from dos formate to unix format. This is very easy
in unix. But in dos environment, the script can't work.

The perl version I used is:5.001.

Anyone knows the clue? thanks in advance,


open(INPUT, "<$opt_f");
rename( $opt_f, "$opt_f.bak") || die "Unable to rename $opt_f\n$!\n";
open(OUTPUT, ">$opt_f");
while(<INPUT>) {
if ( s/\r\n/\n/ ) {
$linesFixed++;
}
print OUTPUT;
}



 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-27-2004
Liang wrote:
> I want to convert a file from dos formate to unix format. This is
> very easy in unix. But in dos environment, the script can't work.


You need to binmode the OUTPUT filehandle, or else the "\r" characters
are reinserted by the OS when you write to OUTPUT.

perldoc -f binmode

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      08-27-2004
Liang wrote:
> I want to convert a file from dos formate to unix format. This is
> very easy in unix. But in dos environment, the script can't work.


> if ( s/\r\n/\n/ ) {


This works onUnix, because on Unix the \r is a Carriage Return (CR) and the
\n a Line Feed (LF) which together happen to be the be Windows newline
identifier and you are replacing them with a Unix newline identifier.

On Windows the \r is a CR, too, but the \n is a combination of CR and LF. So
effectively you are trying to replace CR+CR+LF with CR+LF. Doesn't make much
sense, does it.

From "perldoc perlop":
All systems use the virtual ""\n"" to represent a line terminator,
called a "newline". There is no such thing as an unvarying, physical
newline character. It is only an illusion that the operating system,
device drivers, C libraries, and Perl all conspire to preserve. Not all
systems read ""\r"" as ASCII CR and ""\n"" as ASCII LF. For example, on
a Mac, these are reversed, and on systems without line terminator,
printing ""\n"" may emit no actual data. In general, use ""\n"" when you
mean a "newline" for your system, but use the literal ASCII when you
need an exact character. For example, most networking protocols expect
and prefer a CR+LF (""\015\012"" or ""\cM\cJ"") for line terminators,
and although they often accept just ""\012"", they seldom tolerate just
""\015"". If you get in the habit of using ""\n"" for networking, you
may be burned some day.

jue


 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      08-27-2004
Liang wrote:

> But in dos environment, the script can't work.
>
> The perl version I used is:5.001.


That's ancient. Upgrade to 5.8.x version.

> open(INPUT, "<$opt_f");
> rename( $opt_f, "$opt_f.bak") || die "Unable to rename $opt_f\n$!\n";


Unlike Unix/Linux/Posix, some operating systems do not allow you to
rename a file while it is open. You should open the file after the rename.

-Joe
 
Reply With Quote
 
Liang
Guest
Posts: n/a
 
      08-30-2004
Thanks a lot, it works!

"Gunnar Hjalmarsson" <> wrote in message
news:4AFXc.101923$...
> Liang wrote:
> > I want to convert a file from dos formate to unix format. This is
> > very easy in unix. But in dos environment, the script can't work.

>
> You need to binmode the OUTPUT filehandle, or else the "\r" characters
> are reinserted by the OS when you write to OUTPUT.
>
> perldoc -f binmode
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl



 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-30-2004
Liang wrote:
> Gunnar Hjalmarsson wrote:
>> Liang wrote:
>>> I want to convert a file from dos formate to unix format. This
>>> is very easy in unix. But in dos environment, the script can't
>>> work.

>>
>> You need to binmode the OUTPUT filehandle, or else the "\r"
>> characters are reinserted by the OS when you write to OUTPUT.
>>
>> perldoc -f binmode

>
> Thanks a lot, it works!


That's fine, but please read Jürgen's reply also for a more
accurate/complete description of why.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IOS DoS defense causes DoS to itself:) Igor Mamuziæ Cisco 2 05-19-2006 11:59 PM
Outsmarting DOS C compiler to print to USB printer -- use DOS interrupt? tomhr C Programming 27 01-12-2006 04:09 PM
[newbie]How to install python under DOS and is there any Wxpython can be installed under dos? john san Python 19 02-18-2005 12:05 PM
Unable to run MS-DOS in Windows and DOS properties tabs missing Don Computer Support 5 02-11-2004 07:20 PM
Executing DOS (yes, DOS) program from within Python? Ben Fairbank Python 2 10-07-2003 08:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57