Re: nonprinting chars
by gaal (Parson) on Sep 17, 2004 at 15:07 UTC
|
For cat -A emulation, try this:
perl -pe 's/([\cA-\cI\cK-\cZ])/"^" . chr( ord($1) + 64 )/eg; s/\n/\$\n/s'
Note that cat -A (at least on my system) adds "$"s to mark end of lines.
To shred CRs, use this:
perl -pe 's/\r//'
Update: removed redundant \t treatment. | [reply] [d/l] [select] |
Re: nonprinting chars
by ambrus (Abbot) on Sep 17, 2004 at 15:23 UTC
|
perl -p -i- -e 's/\r\n/\b/g' did not work because
stdin and stdout are in text mode,
so the \r's are discarded when they are read, and are added back when they are output.
Use binmode to avoid that.
| [reply] [d/l] |
|
|
such speed of reply, you guys are great.
trying out all your answers now ...
| [reply] |
Re: nonprinting chars
by skx (Parson) on Sep 17, 2004 at 15:49 UTC
|
Your main question has already been answered, but if you're looking at replicating Unix-like commands in perl you could do worse than look at Perl Power Tools.
This project has many of the standard Unix commands reimplemented as portable perl scripts.
(Normally I'd suggest Cygwin, but using perl is also fun :)
| [reply] |
Re: nonprinting chars
by davido (Cardinal) on Sep 17, 2004 at 15:19 UTC
|
^M at the end of lines is a good indication that you've got a file written on one operating system, moved to another operating system without properly converting line endings. The non-Perl commands, dos2unix, and unix2dos will convert those line endings for you.
Also, use ASCII mode when you do FTP transfers of Perl scripts, or any other plain-text-based file. Most FTP clients will handle the line ending conversion for you.
| [reply] |
Re: nonprinting chars
by Roy Johnson (Monsignor) on Sep 17, 2004 at 15:13 UTC
|
The -l option to perl will auto-chomp input lines and put the appropriate line-ending on output lines.
Does this give you something like what you want to see, as far as making unprintable characters viewable?
while (<>) {
s/([\000-\037])/'^'.chr(ord($1)+0100)/ge;
s/\0177/^?/g;
s/([\200-\377])/sprintf"M-%o", ord($1)/ge;
print;
}
Caution: Contents may have been coded under pressure.
| [reply] [d/l] [select] |
Re: nonprinting chars (binmode)
by tye (Sage) on Sep 17, 2004 at 17:01 UTC
|
In Windows, if you don't use binmode then "\r\n" in a file will become "\n" when the file is read.
I started writing a patch for open.pm to make binmode work with <> and @ARGV, but the related Perl source code makes this hard to do and I'd fix the code by fixing <> to not be magical by default (so that having a file named "rm -rf ..|" was no longer a horrible security problem with Unix Perl) but p5p wants that to remain a horrible security problem so I gave up on it (note that their choice also makes the patch much harder to implement so my reasons for abandoning the project were more than just a moral stance).
| [reply] [d/l] [select] |
Re: nonprinting chars
by Zaxo (Archbishop) on Sep 17, 2004 at 17:12 UTC
|
If you have good unicode fonts and handling, see Printing the Unprintable. It shows how to translate unprintable control characters into the DEC character set for them. You won't need the entity translation I used there for webbiness.
| [reply] |
Re: nonprinting chars
by Your Mother (Archbishop) on Sep 18, 2004 at 03:45 UTC
|
Don't forget this little ditty (IF killing unprintables is what you're after and your Perl is new enough--the \r advice above is great already):
perl -pi.bk -e 's/[^[:print:]]//g'
| [reply] [d/l] |