Solved

Remove CRs in Windows (part 2)

Posted on 2006-06-29
8
463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Six Sigma Control Plans

617 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