replacing nonprintable characters in a file

replacing nonprintable characters in a file

am 04.06.2005 00:21:15 von Eben

Tips from a previous thread helped me with this task. The previous
thread:
http://groups-beta.google.com/group/comp.lang.perl.modules/b rowse_frm/thread/64c79e909fb9cc14/0aa566545d90faa2?q=removin g+NON+PRINTABLE+CHARACTERS&rnum=1&hl=en#0aa566545d90faa2

To give back to the community, my two bits on how to
nonprintable characters in a file. Simple for you perl gurus, but for
perl newbies like me this makes my day!

------

# to replace the non printable characters in a file...

$filename = "/AIH/TEMPDATA/PMFEXT";
$fileout = "/AIH/TEMPDATA/PMFEXT2";
open(OUT, ">$fileout");
open(IN, "<$filename");

$_ = ; #read the first line
s/[^[:print:]]/ /g;
print OUT "$_";

while() { #loop as long as not EOF
s/[^[:print:]]/ /g;
print OUT "$_";
}

------

Re: replacing nonprintable characters in a file

am 04.06.2005 01:06:13 von John Bokma

Eben wrote:

> Tips from a previous thread helped me with this task. The previous
> thread:
> http://groups-
beta.google.com/group/comp.lang.perl.modules/browse_frm/t
> hread/64c79e909fb9cc14/0aa566545d90faa2?
q=removing+NON+PRINTABLE+CHARAC
> TERS&rnum=1&hl=en#0aa566545d90faa2
>
> To give back to the community, my two bits on how to
> nonprintable characters in a file. Simple for you perl gurus, but for
> perl newbies like me this makes my day!
>
> ------
>
> # to replace the non printable characters in a file...
>
> $filename = "/AIH/TEMPDATA/PMFEXT";
> $fileout = "/AIH/TEMPDATA/PMFEXT2";
> open(OUT, ">$fileout");

check

> open(IN, "<$filename");

check

> $_ = ; #read the first line
> s/[^[:print:]]/ /g;
> print OUT "$_";

why did you do this, since you are going to loop?

> while() { #loop as long as not EOF
> s/[^[:print:]]/ /g;
> print OUT "$_";

no need to quote $_

> }



--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Re: replacing nonprintable characters in a file

am 04.06.2005 08:08:20 von ___cliff rayman___

Eben wrote:

>
>$filename = "/AIH/TEMPDATA/PMFEXT";
>$fileout = "/AIH/TEMPDATA/PMFEXT2";
>open(OUT, ">$fileout");
>open(IN, "<$filename");
>
>
If you are on a unix style box, I tend to use pipes. This way we can
use the same program on a bunch of differnt files. So I would not name
the file in the program and instead run it this way on the unix command
line:
$ print_printables.pl < /AIH/TEMPDATA/PMFEXT > /AIH/TEMPDATA/PMFEXT2 #
don't really need the "less than" sign

>$_ = ; #read the first line
>s/[^[:print:]]/ /g;
>print OUT "$_";
>
>
not sure why you are doing the above when all data will be handled in
the loop below

>while() { #loop as long as not EOF
>s/[^[:print:]]/ /g;
>print OUT "$_";
>}
>
>
because we are using pipes we will do it like this
#!/usr/bin/perl
while (<>){
s/[^[:print:]]/ /g;
print;
}

or, I could write the whole entire program like this in two (count them)
total lines
#!/usr/bin/perl -p
s/[^[:print:]]/ /g;

the -p after the perl run sheband causes the program to loop through
STDIN (the piped in data), and print the line after any processing that
we provide.

this is why we love perl.

by the way - you are not using modules, and therefore, this is not the
correct place to ask this question. next time try perlmonks.com or
comp.language.perl.moderated

--
_____cliff_rayman_____________________________________
Business Consulting and Turnaround Management
[web] http://www.rayman.com/
[web] http://all-clear-turnaround-management.com/
[eml] cliff _at_ rayman.com
[phn] 888-736-3802 x701
[fax] 818-743-7404
______________________________________________________