Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Remove CRs in Windows (part 2)

Posted on 2006-06-29
8
Medium Priority
?
464 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 25

Accepted Solution

by:
clockwatcher earned 240 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

688 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