Solved

Remove CRs in Windows (part 2)

Posted on 2006-06-29
8
458 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl, group, sort, count question 6 118
Question about @INC variable in perl 1 55
perl to convert excel to csv 3 184
Perl Write to Specific line in a file 15 76
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now