Click here to get back home

replacing nonprintable characters in a file

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
replacing nonprintable characters in a file Eben 06-03-2005
Posted by Eben on June 3, 2005, 4:21 pm
Please log in for more thread options


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/thread/64c79e909fb9cc14/0aa566545d90faa2?q=removing+NON+PRINTABLE+CHARACTERS&rnum=1&hl=en#0aa566545d90faa2

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

---<code begin>---

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

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

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

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

---<code end>---



Posted by John Bokma on June 4, 2005, 12:06 am
Please log in for more thread options


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 <replace>
> nonprintable characters in a file. Simple for you perl gurus, but for
> perl newbies like me this makes my day!
>
> ---<code begin>---
>
> # 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

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

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

> while(<IN>) { #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



Posted by ___cliff rayman___ on June 4, 2005, 7:08 am
Please log in for more thread options


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

>$_ = <IN>; #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(<IN>) { #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
______________________________________________________


Similar ThreadsPosted
replacing characters with their ASCII codes August 20, 2005, 8:50 pm
Reusing SQL data-related logic, replacing weird modules and improving Class::DBI July 2, 2005, 1:23 am
Removing non-printing characters ... October 7, 2004, 8:47 pm
handling UTF-8 characters in LWP module August 31, 2006, 10:39 pm
regular expression problem ? and * characters May 28, 2006, 7:02 am
Spreadsheet::Read special characters handling November 20, 2006, 2:33 am
Print Subject without characters Just Numbers using POP3Client. Please Help April 10, 2007, 11:27 pm
Do Win32::ODBC module support Chinese characters when used with MS Access? September 15, 2004, 10:33 pm
XP, Perl, Win32::ODBC, Microsoft Access 2002 SP3, & Chinese characters January 22, 2008, 4:18 pm
install HTML::Template - Problem reading cache file / Bad file number July 24, 2004, 7:55 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap