Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

removing all whitespace but newlines

by blacksmith (Hermit)
on Jan 28, 2003 at 19:18 UTC ( [id://230693]=perlquestion: print w/replies, xml ) Need Help??

blacksmith has asked for the wisdom of the Perl Monks concerning the following question:

Okay I have resolved my initial problem. Actually resolved it because writing this gave me an idea to a different approach to resolution. This is what I had going on. I have a .txt file that I am trying to upload to a mainframe for processing. Each record in the file should only be 12 characters long. But, the file that was sent to me to upload has a large amount of whitespace (this includes one \n) trailing the 12 digit record. The FTP step in the mainframe job rejects the file since it is looking for a 12 digit record. So here is what I am trying to do. I am trying to find a way to delete to "spaces" and leave the newline. For some reason everytime I negate \n it doesn't work correctly. Or, I should say the way I want it to work. Perl doesn't lie. I always works correctly. I just don't always script correctly. I went about fixing this issue by using sprintf. I felt this would be more acurate since the record length must be 12, no more, no less. But, I am still curious. How do you delete all whitespace but the newline?
blacksmith

Replies are listed 'Best First'.
Re: removing all whitespace but newlines
by thelenm (Vicar) on Jan 28, 2003 at 19:29 UTC
    Perhaps something like this, which will remove all whitespace except for newlines, if your line is in $line.
    $line =~ s/[^\S\n]+//g;
    If you need to remove just trailing whitespace, change it to this:
    $line =~ s/[^\S\n]+$//g;

    Update: I should point out explicitly that that's a capital S inside the character class... the character class matches anything that's non-non-whitespace (i.e., whitespace) and non-newline.

    -- Mike

    --
    just,my${.02}

Re: removing all whitespace but newlines
by Abigail-II (Bishop) on Jan 28, 2003 at 22:02 UTC
    One way of doing so would be:
    perl -wi -lpe 's/[[:space:]]+//g' file

    Note the use of [:space:], the POSIX character class that includes all the whitespace characters. \s will not do, as that does not include the vertical tab character. Newlines won't be removed by the code above, as they are chopped off when reading a line, and added back on when printing it. It's the -l that will do this.

    Abigail

Re: removing all whitespace but newlines
by ibanix (Hermit) on Jan 28, 2003 at 19:34 UTC
    How do you delete all whitespace but the newline?

    A simple and probally inefficent way to do it, assuming you're processing one line at a time:

    $line =~ s/\s+//g; # But this replaces \n too, so add it back on the end $line . "\n";

    Cheers,
    ibanix

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
Re: (nrd) removing all whitespace but newlines
by newrisedesigns (Curate) on Jan 28, 2003 at 19:31 UTC

    Does each field have only A-Z0-9 characters?

    If so,
    $_ =~ /((\w{12})\n)/;
    $1 = the 12 digit field plus the newline, while $2 = the 12 digit field without.

    If you need other characters, you could use
    $_ =~ /(([A-Za-z0-9\.\+\-]{12})\n)/;
    To get alphanumerics and '.', '+', and '-'.

    John J Reiser
    newrisedesigns.com

Re: removing all whitespace but newlines
by Gilimanjaro (Hermit) on Jan 28, 2003 at 20:17 UTC
    Or using my newlyfound perlrun knowledge:

    When there never are spaces in the end of the 12 characters, and you're sure all lines do contain 12 non-space characters:

    perl -i -lpe 's/\s+^//' myfile

    Or the foolproof way:

    perl -lne '/^.{1,12}/; printf "%-12s\n",$&' myfile

    I think I just started playing perlgolf...

Re: removing all whitespace but newlines
by integral (Hermit) on Jan 28, 2003 at 19:36 UTC
    One solution, assuming that the twelve chars are at the start of each line:
    local $\ = "\n"; while(<>) { print substr $_, 0, 12 }

    -- integral, resident of freenode's #perl

Re: removing all whitespace but newlines
by blacksmith (Hermit) on Jan 28, 2003 at 22:21 UTC
    Thanks fellow monks. It's been a great experience seeing some of the different ideas. Really helped stimulate my mind to learn new things!!
    blacksmith.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://230693]
Approved by newrisedesigns
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-30 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found