Solved

Remove CRs in Windows (part 2)

Posted on 2006-06-29
8
456 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 11

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

14 Experts available now in Live!

Get 1:1 Help Now