Solved

Remove CRs in Windows (part 2)

Posted on 2006-06-29
8
460 Views
Last Modified: 2008-03-06
Hi Experts,

Some time ago, ozo showed me how to do things like:
    perl -pe "BEGIN{binmode STDOUT} s/\r//" dosfile >unixfile
to remove CRs (carriage returns) from files, and:
    perl -i.bak -pe "BEGIN{@ARGV=<@ARGV} s/a/b/" *.html
to change "a" to "b" in all html files.

However, I'm trying to do a combination of both, but I can't get it to work.  I'm trying to remove all CRs from all html files in the current directory.  Here's the code I'm trying:
    perl -i.bak -pe "BEGIN{@ARGV=<@ARGV>;binmode STDOUT} s/\r//" *.html
The result is, the new files are created identical to the .bak files (I have checked the exact byte counts, and seen that they all contain line endings of hex 0D 0A).

What am I doing wrong?

Please remember that I'm working in Windows (maybe that's what I'm doing wrong!), and I want a one-liner.

Thanks.
0
Comment
Question by:tel2
8 Comments
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17015317
0D 0D is a newline (\n), buit you're looking for a return (\r).  Change your regular expression to s/\r?\n//g.  You could also try s/\x0D\x0A//g.
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17015325
typo: "0D 0D is a newline (\n)..." should read "0D 0A is a newline (\n)...".  Sorry.
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17015518
Give this a try:

  perl -i.bak -pe "BEGIN{@ARGV=<@ARGV>;} binmode ARGVOUT; s/\r//s" *.html
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 25

Accepted Solution

by:
clockwatcher earned 60 total points
ID: 17015525
Sorry... forgot to chop the /s off the end of that replace (was messing with stuff and forgot to cut it from the copy and paste).  It'll work with it in there but doesn't need to be.

  perl -i.bak -pe "BEGIN{@ARGV=<@ARGV>;} binmode ARGVOUT; s/\r//" *.html
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17015564
And I'm not sure if there's a problem with calling binmode repeatedly like I did, but you might want to go with:

  perl -i.bak -pe "BEGIN{@ARGV=<@ARGV>; $f=1} binmode ARGVOUT if $f; s/\r//; $f = eof;" *.html
0
 
LVL 39

Expert Comment

by:Adam314
ID: 17017938
clockwatcher,

why not put the binmode in the begin?
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17019442
I believe the in-place opens the files in the implicit (<>) block-- post the begin.   I'm pretty sure.  Too tired to give it a try right now.  But, it seems like it'd have to be opening them there.  Pretty sure I tried it last night and that's what I ran into-- and what makes sense.
0
 
LVL 12

Author Comment

by:tel2
ID: 17035079
Hi mjcoyne,
I've tried:
    perl -pe "s/\r?\n//g" dosfile >unixfile
which just removes all line endings, and in this case seems to be equivalent to:
    perl -pe "s/\n//g" dosfile >unixfile
in DOS.  And this:
    perl -pe "s/\x0D\x0A//g" dosfile >unixfile
doesn't do anything, but this:
    perl -pe "s/\x0A//g" dosfile >unixfile
removes all line endings.  Same result in binmode.  I don't think my issue is to do with the regex.  I already had a regex which worked, but just not for in-place substitution.  I don't want to remove all line endings.  I just want to change \r\n (DOS type) line endings to \n (UNIX type) line endings.  But I appreciate your efforts.

Hi clockwatcher,
Your solutions work well, thank you!  And you seem to be right about the binmode setting taking effect after the BEGIN, because:
    perl -i.bak -pe "BEGIN {binmode ARGVOUT} s/\r//" dosfile
leaves dosfile unchanged, but:
    perl -i.bak -pe "binmode ARGVOUT; s/\r//" dosfile
removes the \r's from dosfile, as required.
Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 182
delete query using perl dbi 3 108
compress files in RAR using perl 13 83
Regular Expression question to filter with negation. 6 100
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question