Ask a Question related to PERL Beginners, Design and Development.
-
Marcos Rebelo #1
RE: Getting rid of white space...
are you speaking of this?
use strict;
while(my $line = <>) {
($line) = ($line =~ /^\s*(.*)\s*\n$/);
print($line."_nice/n");
}
-----Original Message-----
From: LoneWolf [mailto:lonewolf@nc.rr.com]
Sent: Thursday, September 04, 2003 3:38 PM
To: [email]beginners@perl.org[/email]
Subject: Getting rid of white space...
I have about 12 files that I am pulling for a SCO box to a RedHat box, FTP.
THe files from the SCO box are poorly formatted with extraneous whitespace
(sometimes as much as 30 or more) before and after the text. I need to
parse all of the files I DL and put them into a new file with "_nice" added
at the end.
The files are all pipe-delimited, so I don't have a problem separating the
fields, I just am not sure how to make it remove all extra whitespace. It
needs to keep all Space in the fields " the description of the
file " should still be readable as "the description of the file"
Any help with code examples? I have been looking through a beginning book
and my old code and have come up nil.
Thanks,
Robert
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
Marcos Rebelo Guest
-
Logo on Web - White Space
Using a corporate logo on my web page. It's a TIF. I can change the color of the logo graphic (round, but on the web page the logo displays on a... -
how to contract white space
I started using Director 7 on PC and I am unable to contract white space around my cast members. The casts members are are drawn by pencil and are... -
Trim White Space
I imported a bitmap with white spaces at the borders whithout trimming it. That's right, it's gone fine, i have my image with the white spaces. But... -
White space around the whole page
How can I get rid of the white space around my page, when I preview it in IE6? -
White Space around pop-up window
Hi All I have created a bunch of thumbnail images that when clicked on would open up a new window. I have sized the window pop-up to be the exact... -
Anthony Akens #2
RE: Getting rid of white space...
I figured I'd take a stab at fleshing this out into what he wants...
Any comments on things I could do better? I only added to what
Robert had coded...
Tony
#!/usr/bin/perl -w
use strict;
my $dirname = "/my/stuff/";
my $file;
my $newfile;
my $line;
opendir (DIR, $dirname) or die "Can't opendir $dirname: $!";
while (defined($file = readdir(DIR))) {
next if $file =~ /^\.\.?$/;
open (OLDFILE, "< $file");
$newfile = $file . "_nice";
open (NEWFILE, "> $newfile");
while ($line = <OLDFILE>) {
($line) = ($line =~ /^\s*(.*)\s*\n$/);
print NEWFILE "$line\n";
}
close OLDFILE;
close NEWFILE;
}
-----Original Message-----
From: [email]Marcos.Rebelo@eurocopter.com[/email] [mailto:Marcos.Rebelo@eurocopter.com]
Sent: Thursday, September 04, 2003 8:32 AM
To: [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
Subject: RE: Getting rid of white space...
are you speaking of this?
use strict;
while(my $line = <>) {
($line) = ($line =~ /^\s*(.*)\s*\n$/);
print($line."_nice/n");
}
-----Original Message-----
From: LoneWolf [mailto:lonewolf@nc.rr.com]
Sent: Thursday, September 04, 2003 3:38 PM
To: [email]beginners@perl.org[/email]
Subject: Getting rid of white space...
I have about 12 files that I am pulling for a SCO box to a RedHat box, FTP.
THe files from the SCO box are poorly formatted with extraneous whitespace
(sometimes as much as 30 or more) before and after the text. I need to
parse all of the files I DL and put them into a new file with "_nice" added
at the end.
The files are all pipe-delimited, so I don't have a problem separating the
fields, I just am not sure how to make it remove all extra whitespace. It
needs to keep all Space in the fields " the description of the
file " should still be readable as "the description of the file"
Any help with code examples? I have been looking through a beginning book
and my old code and have come up nil.
Thanks,
Robert
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
Anthony Akens Guest
-
Brian Harnish #3
Re: Getting rid of white space...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Thu, 04 Sep 2003 06:37:57 -0700, LoneWolf wrote:
- From your example it appears that you want to remove all whitespace from> I have about 12 files that I am pulling for a SCO box to a RedHat box, FTP.
> THe files from the SCO box are poorly formatted with extraneous whitespace
> (sometimes as much as 30 or more) before and after the text. I need to
> parse all of the files I DL and put them into a new file with "_nice" added
> at the end.
>
> The files are all pipe-delimited, so I don't have a problem separating the
> fields, I just am not sure how to make it remove all extra whitespace. It
> needs to keep all Space in the fields " the description of the
> file " should still be readable as "the description of the file"
>
> Any help with code examples? I have been looking through a beginning book
> and my old code and have come up nil.
the beginning, and end, and duplicates from inside. You can achieve this
easily:
my $text = ' the description of the file ';
for($text) {
s/^\s+//;
s/\s+$//;
# Perhaps its more effecient to put this next line first?
s/\s+/ /g;
}
Also, check "perldoc -q space".
- Brian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE/V1ohiK/rA3tCpFYRAsGpAJ0XWKLjN9+7MYBnfeFz1g/DBWsqtgCg80v1
COz0Otn5XsqmO3D/ErXKkjQ=
=EgAZ
-----END PGP SIGNATURE-----
Brian Harnish Guest
-
Stephen Marshall #4
RE: Getting rid of white space...
I have a similar problem to this , does anyone have another answer? Don't know if its just me but The
Code doesn't work , and the enhanced version with the fancy file handling doesn't work either "trying to read from a closed filehandle error"> use strict;
> while(my $line = <>) {
> ($line) = ($line =~ /^\s*(.*)\s*\n$/);
> print($line."_nice/n");
> }
Stephen
> -----Original Message-----
> From: Akens, Anthony [mailto:AAkens@egh.org]
> Sent: 04 September 2003 15:55
> To: [email]Marcos.Rebelo@eurocopter.com[/email]; [email]LoneWolf@nc.rr.com[/email];
> [email]beginners@perl.org[/email]
> Subject: RE: Getting rid of white space...
>
>
> I figured I'd take a stab at fleshing this out into what he
> wants... Any comments on things I could do better? I only
> added to what Robert had coded...
>
> Tony
>
>
>
>
> #!/usr/bin/perl -w
>
> use strict;
> my $dirname = "/my/stuff/";
> my $file;
> my $newfile;
> my $line;
>
> opendir (DIR, $dirname) or die "Can't opendir $dirname: $!";
> while (defined($file = readdir(DIR))) {
> next if $file =~ /^\.\.?$/;
> open (OLDFILE, "< $file");
> $newfile = $file . "_nice";
> open (NEWFILE, "> $newfile");
> while ($line = <OLDFILE>) {
> ($line) = ($line =~ /^\s*(.*)\s*\n$/);
> print NEWFILE "$line\n";
> }
> close OLDFILE;
> close NEWFILE;
> }
>
>
> -----Original Message-----
> From: [email]Marcos.Rebelo@eurocopter.com[/email]
> [mailto:Marcos.Rebelo@eurocopter.com]
> Sent: Thursday, September 04, 2003 8:32 AM
> To: [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
> Subject: RE: Getting rid of white space...
>
>
> are you speaking of this?
>
> use strict;
> while(my $line = <>) {
> ($line) = ($line =~ /^\s*(.*)\s*\n$/);
> print($line."_nice/n");
> }
>
> -----Original Message-----
> From: LoneWolf [mailto:lonewolf@nc.rr.com]
> Sent: Thursday, September 04, 2003 3:38 PM
> To: [email]beginners@perl.org[/email]
> Subject: Getting rid of white space...
>
>
> I have about 12 files that I am pulling for a SCO box to a
> RedHat box, FTP.
> THe files from the SCO box are poorly formatted with
> extraneous whitespace (sometimes as much as 30 or more)
> before and after the text. I need to parse all of the files
> I DL and put them into a new file with "_nice" added at the end.
>
> The files are all pipe-delimited, so I don't have a problem
> separating the fields, I just am not sure how to make it
> remove all extra whitespace. It
> needs to keep all Space in the fields " the
> description of the
> file " should still be readable as "the description
> of the file"
>
> Any help with code examples? I have been looking through a
> beginning book and my old code and have come up nil.
>
> Thanks,
> Robert
>
> --
> To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> For additional commands, e-mail: [email]beginners-help@perl.org[/email]
>
> --
> To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> For additional commands, e-mail: [email]beginners-help@perl.org[/email]
>
>
> --
> To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> For additional commands, e-mail: [email]beginners-help@perl.org[/email]
>Stephen Marshall Guest
-
Stephen Marshall #5
RE: Getting rid of white space...
Got it working this way fror the important line, but theres probably a slicker way of doing it.
$line =~ s/(\s)+/ /g;
> -----Original Message-----
> From: Marshall, Stephen
> Sent: 04 September 2003 17:07
> To: 'Akens, Anthony'; [email]Marcos.Rebelo@eurocopter.com[/email];
> [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
> Subject: RE: Getting rid of white space...
>
>
> I have a similar problem to this , does anyone have another
> answer? Don't know if its just me but The
>>> > use strict;
> > while(my $line = <>) {
> > ($line) = ($line =~ /^\s*(.*)\s*\n$/);
> > print($line."_nice/n");
> > }
> Code doesn't work , and the enhanced version with the fancy
> file handling doesn't work either "trying to read from a
> closed filehandle error"
>
> Stephen
>>> > -----Original Message-----
> > From: Akens, Anthony [mailto:AAkens@egh.org]
> > Sent: 04 September 2003 15:55
> > To: [email]Marcos.Rebelo@eurocopter.com[/email]; [email]LoneWolf@nc.rr.com[/email];
> > [email]beginners@perl.org[/email]
> > Subject: RE: Getting rid of white space...
> >
> >
> > I figured I'd take a stab at fleshing this out into what he
> > wants... Any comments on things I could do better? I only
> > added to what Robert had coded...
> >
> > Tony
> >
> >
> >
> >
> > #!/usr/bin/perl -w
> >
> > use strict;
> > my $dirname = "/my/stuff/";
> > my $file;
> > my $newfile;
> > my $line;
> >
> > opendir (DIR, $dirname) or die "Can't opendir $dirname: $!";
> > while (defined($file = readdir(DIR))) {
> > next if $file =~ /^\.\.?$/;
> > open (OLDFILE, "< $file");
> > $newfile = $file . "_nice";
> > open (NEWFILE, "> $newfile");
> > while ($line = <OLDFILE>) {
> > ($line) = ($line =~ /^\s*(.*)\s*\n$/);
> > print NEWFILE "$line\n";
> > }
> > close OLDFILE;
> > close NEWFILE;
> > }
> >
> >
> > -----Original Message-----
> > From: [email]Marcos.Rebelo@eurocopter.com[/email]
> > [mailto:Marcos.Rebelo@eurocopter.com]
> > Sent: Thursday, September 04, 2003 8:32 AM
> > To: [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
> > Subject: RE: Getting rid of white space...
> >
> >
> > are you speaking of this?
> >
> > use strict;
> > while(my $line = <>) {
> > ($line) = ($line =~ /^\s*(.*)\s*\n$/);
> > print($line."_nice/n");
> > }
> >
> > -----Original Message-----
> > From: LoneWolf [mailto:lonewolf@nc.rr.com]
> > Sent: Thursday, September 04, 2003 3:38 PM
> > To: [email]beginners@perl.org[/email]
> > Subject: Getting rid of white space...
> >
> >
> > I have about 12 files that I am pulling for a SCO box to a
> > RedHat box, FTP.
> > THe files from the SCO box are poorly formatted with
> > extraneous whitespace (sometimes as much as 30 or more)
> > before and after the text. I need to parse all of the files
> > I DL and put them into a new file with "_nice" added at the end.
> >
> > The files are all pipe-delimited, so I don't have a problem
> > separating the fields, I just am not sure how to make it
> > remove all extra whitespace. It
> > needs to keep all Space in the fields " the
> > description of the
> > file " should still be readable as "the description
> > of the file"
> >
> > Any help with code examples? I have been looking through a
> > beginning book and my old code and have come up nil.
> >
> > Thanks,
> > Robert
> >
> > --
> > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
> >
> > --
> > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
> >
> >
> > --
> > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
> >Stephen Marshall Guest
-
Bob Showalter #6
RE: Getting rid of white space...
LoneWolf wrote:
It's a little unclear what you're asking for. You say you want to keep all> I have about 12 files that I am pulling for a SCO box to a RedHat
> box, FTP. THe files from the SCO box are poorly formatted with
> extraneous whitespace (sometimes as much as 30 or more) before and
> after the text. I need to parse all of the files I DL and put them
> into a new file with "_nice" added at the end.
>
> The files are all pipe-delimited, so I don't have a problem
> separating the fields, I just am not sure how to make it remove all
> extra whitespace. It needs to keep all Space in the fields "
> the description of the
> file " should still be readable as "the description of the
> file"
space in the fields, but your example compresses multiple spaces between
words into single spaces.
Anyway, assuming the following:
1. You have split the fields into an array, say @fields, and
2. You want to remove all leading and trailing whitespace, and
3. You want to compress multiple internal whitespace chars into a single
space
I would do:
s/^\s+//, s/\s+$//, s/\s\s+/ /g for @fields;
The first two regexes are the canonical way to remove leading and trailing
whitespace. The third compresses the internal whitespace.
>
> Any help with code examples? I have been looking through a beginning
> book and my old code and have come up nil.Bob Showalter Guest
-
Anthony Akens #7
RE: Getting rid of white space...
Try it like this... I was a slacker and didn't have warnings
in place if it couldn't open the file... Should be able
to just change the "/my/stuff/" to your directory and have it
work, I tested it here, and it seems to go fine. (on an aix
box - maybe I have something in place that doesn't work on
windows or other platforms?)
Tony
#!/usr/bin/perl -w
use strict;
my $dirname = "/my/stuff/";
my $file;
my $newfile;
my $line;
opendir (DIR, $dirname) or die "Can't opendir $dirname: $!";
while (defined($file = readdir(DIR))) {
next if $file =~ /^\.\.?$/;
open (OLDFILE, "< $file") or die "Can't open $file: $!";
$newfile = $file . "_nice";
open (NEWFILE, "> $newfile") or die "Can't open $newfile: $!";
while ($line = <OLDFILE>) {
($line) = ($line =~ /^\s*(.*)\s*\n$/);
print NEWFILE "$line\n";
}
close OLDFILE;
close NEWFILE;
}
-----Original Message-----
From: Marshall, Stephen [mailto:stephen.marshall@thus.net]
Sent: Thursday, September 04, 2003 11:11 AM
To: Marshall, Stephen; Akens, Anthony; 'Marcos.Rebelo@eurocopter.com';
'LoneWolf@nc.rr.com'; 'beginners@perl.org'
Subject: RE: Getting rid of white space...
Got it working this way fror the important line, but theres probably a slicker way of doing it.
$line =~ s/(\s)+/ /g;
> -----Original Message-----
> From: Marshall, Stephen
> Sent: 04 September 2003 17:07
> To: 'Akens, Anthony'; [email]Marcos.Rebelo@eurocopter.com[/email];
> [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
> Subject: RE: Getting rid of white space...
>
>
> I have a similar problem to this , does anyone have another
> answer? Don't know if its just me but The
>>> > use strict;
> > while(my $line = <>) {
> > ($line) = ($line =~ /^\s*(.*)\s*\n$/);
> > print($line."_nice/n");
> > }
> Code doesn't work , and the enhanced version with the fancy
> file handling doesn't work either "trying to read from a
> closed filehandle error"
>
> Stephen
>>> > -----Original Message-----
> > From: Akens, Anthony [mailto:AAkens@egh.org]
> > Sent: 04 September 2003 15:55
> > To: [email]Marcos.Rebelo@eurocopter.com[/email]; [email]LoneWolf@nc.rr.com[/email];
> > [email]beginners@perl.org[/email]
> > Subject: RE: Getting rid of white space...
> >
> >
> > I figured I'd take a stab at fleshing this out into what he
> > wants... Any comments on things I could do better? I only
> > added to what Robert had coded...
> >
> > Tony
> >
> >
> >
> >
> > #!/usr/bin/perl -w
> >
> > use strict;
> > my $dirname = "/my/stuff/";
> > my $file;
> > my $newfile;
> > my $line;
> >
> > opendir (DIR, $dirname) or die "Can't opendir $dirname: $!";
> > while (defined($file = readdir(DIR))) {
> > next if $file =~ /^\.\.?$/;
> > open (OLDFILE, "< $file");
> > $newfile = $file . "_nice";
> > open (NEWFILE, "> $newfile");
> > while ($line = <OLDFILE>) {
> > ($line) = ($line =~ /^\s*(.*)\s*\n$/);
> > print NEWFILE "$line\n";
> > }
> > close OLDFILE;
> > close NEWFILE;
> > }
> >
> >
> > -----Original Message-----
> > From: [email]Marcos.Rebelo@eurocopter.com[/email]
> > [mailto:Marcos.Rebelo@eurocopter.com]
> > Sent: Thursday, September 04, 2003 8:32 AM
> > To: [email]LoneWolf@nc.rr.com[/email]; [email]beginners@perl.org[/email]
> > Subject: RE: Getting rid of white space...
> >
> >
> > are you speaking of this?
> >
> > use strict;
> > while(my $line = <>) {
> > ($line) = ($line =~ /^\s*(.*)\s*\n$/);
> > print($line."_nice/n");
> > }
> >
> > -----Original Message-----
> > From: LoneWolf [mailto:lonewolf@nc.rr.com]
> > Sent: Thursday, September 04, 2003 3:38 PM
> > To: [email]beginners@perl.org[/email]
> > Subject: Getting rid of white space...
> >
> >
> > I have about 12 files that I am pulling for a SCO box to a
> > RedHat box, FTP.
> > THe files from the SCO box are poorly formatted with
> > extraneous whitespace (sometimes as much as 30 or more)
> > before and after the text. I need to parse all of the files
> > I DL and put them into a new file with "_nice" added at the end.
> >
> > The files are all pipe-delimited, so I don't have a problem
> > separating the fields, I just am not sure how to make it
> > remove all extra whitespace. It
> > needs to keep all Space in the fields " the
> > description of the
> > file " should still be readable as "the description
> > of the file"
> >
> > Any help with code examples? I have been looking through a
> > beginning book and my old code and have come up nil.
> >
> > Thanks,
> > Robert
> >
> > --
> > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
> >
> > --
> > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
> >
> >
> > --
> > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
> >Anthony Akens Guest
-
Alan Perry #8
RE: Getting rid of white space...
On Thursday, September 04, 2003 11:11, Marshall, Stephen wrote:
slicker way of doing it.>
>Got it working this way fror the important line, but theres probably aThis will work, but may leave an extraneous space at the beginning and/or>
>$line =~ s/(\s)+/ /g;
>
end of the line.
This text:
" Test text with lots of extra spaces "
would get changed to:
" Test text with lots of extra spaces "
which may not be what you want.
If you want to eliminate any starting or ending space and trim the rest of
it down to single spaces, I would suggest this:
$line =~ s/\s+/ /g; # the parens you had are not necessary
$line =~ s/^ //; # removes any space from the beginning of the line
$line =~ s/ $//; # removes any space from the end of the line
You could probably get fancier on the statements, but I prefer the
simplicity of three separate statements.
HTH,
Alan
Alan Perry Guest
-
John W. Krahn #9
Re: Getting rid of white space...
Lonewolf wrote:
#!/usr/bin/perl>
> I have about 12 files that I am pulling for a SCO box to a RedHat box, FTP.
> THe files from the SCO box are poorly formatted with extraneous whitespace
> (sometimes as much as 30 or more) before and after the text. I need to
> parse all of the files I DL and put them into a new file with "_nice" added
> at the end.
>
> The files are all pipe-delimited, so I don't have a problem separating the
> fields, I just am not sure how to make it remove all extra whitespace. It
> needs to keep all Space in the fields " the description of the
> file " should still be readable as "the description of the file"
>
> Any help with code examples? I have been looking through a beginning book
> and my old code and have come up nil.
use warnings;
use strict;
( $\, $^I ) = ( $/, '.bak' );
while ( <> ) {
s/^\s+//; # remove space at beginning of line
s/\s+$//; # remove space at end of line
s/\s*\|\s*/|/g; # remove space around pipe separator
print;
if ( eof ) {
close ARGV;
rename $ARGV, "${ARGV}_nice" or warn "Cannot rename $ARGV to ${ARGV}_nice: $!";
}
}
__END__
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
R. Joseph Newton #10
Re: Getting rid of white space...
"Akens, Anthony" wrote:
[Original post repositioned for readability]>> > The files are all pipe-delimited, so I don't have a problem separating the
> > fields, I just am not sure how to make it remove all extra whitespace. It
> > needs to keep all Space in the fields " the description of the> > file " should still be readable as "the description of the file"
> >
> > Any help with code examples? I have been looking through a beginning book
> > and my old code and have come up nil.
Way too complicated. The specifications are much more simple: "Remove> I figured I'd take a stab at fleshing this out into what he wants...
> Any comments on things I could do better? I only added to what
> Robert had coded...
>
> Tony
>
> #!/usr/bin/perl -w
>
> use strict;
> my $dirname = "/my/stuff/";
> my $file;
> my $newfile;
> my $line;
>
> opendir (DIR, $dirname) or die "Can't opendir $dirname: $!";
> while (defined($file = readdir(DIR))) {
> next if $file =~ /^\.\.?$/;
> open (OLDFILE, "< $file");
> $newfile = $file . "_nice";
> open (NEWFILE, "> $newfile");
> while ($line = <OLDFILE>) {
> ($line) = ($line =~ /^\s*(.*)\s*\n$/);
> print NEWFILE "$line\n";
> }
> close OLDFILE;
> close NEWFILE;
> }
extraneous whitespace". This does not reuire any capturing parentheses. Deal
only with whitespace:
while (<IN>) {
chomp;
s/^\s*//; #trim start
s/\s*$/; #trim end
s/\s(2,)/ /g; # eliminate extraneous spce in between
print OUT "$_\n";
}
More lines here, but a helluva lot less processing.
Joseph
R. Joseph Newton Guest



Reply With Quote

