On Mon, Jul 28, 2003 at 09:27:09AM -0500, McKown, John wrote:
> Is there a simple way to strip blanks from all the lines in a file? What I
> am doing is downloading members from my various PDSes. Some of these member
> contain the "standard" sequence number in columns 73-80. I am using Perl to
> strip these off. After stripping off the sequence numbers, I would like to
> strip off any resulting trailing blanks. My current Perl script looks like:
To strip blanks from the end of a string in Perl, a good regular expression
to use is:
s/\s+$//
The \s means "match any whitespace character", including space, tab,
newline, carriage return, and formfeed. The + character means "match one or
more of the preceeding", and the $ means "anchor this regular expression at
the end of the string. Together, this will find one or more whitespace
characters at the end of the string, and remove them.
> #!/usr/bin/perl
> while(<>) {
> chop;
> s/(.{0,72}).*/\1/;
> $new1 = reverse($_);
> $_ = $new1;
> s/ *(.*)/\1/;
> $new = reverse($_);
> print $new,"\n";
> }
You can clean this up a little by using substr() and the regular expression
described above. The code below uses the =~ operator, which binds the
regular expression to the variable $new, instead of to Perl's magic $_
variable.
#!/usr/bin/perl -w
while(<>) {
$new = substr($_, 0, 72); # put first 72 characters into $new
$new =~ s/\s+$//; # Remove trailing space, tab or newline
print "$new\n";
}
Dave
--
('> Dave O'Neill <[EMAIL PROTECTED]>, Staff Scientist, Linuxcare, Inc.
//\ tel: (613) 562-9949 fax: (613) 562-9304 http://www.linuxcare.com/
v_/_ Linuxcare. Simplifying Server Consolidation