#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    8
    Rep Power
    0

    Unhappy Remove white space and carriage returns, line feeds


    I have a perl script that reads a $message variable which is typed in by the user.

    We was trying to remove all white spaces that could be in front, all white spaced at the end, extra spaced inbetween words (For example 2 spaces or more between works would be reduced to 1 space) and to remove any carriage returns or line feeds.

    I am very new to Perl and have been unable to figure this out.

    What I had so far was this....
    $message =~ s/[\n\r\s]+//g;
    But this is removing all white space even between words.

    This is the input it was given...
    ottocat:/lcl/apps/esm/bin>postEvent.pl -s 60 -m ' MESSAGE GOES HERE ' -g 'INFR_Apps' -i 'ISOC INSTRUCTIONS GO HERE' -t -p 'Test_Program' Testing

    The -m is the message text.

    However the output results in...
    MESSAGEGOESHERE

    It should say "MESSAGE GOES HERE"

    Could somebody show me the correct format to use to remove....

    1.) Front white spaces
    2.) Carriage returns
    3.) Line feeds
    4.) End white spaces
    5.) Any extra white spaces between words.
    So for example if it said...
    "This is a test."
    It would remove the extra white space between "is" and "a".

    Thank you
  2. #2
  3. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    79
    Rep Power
    138
    In three steps:
    perl Code:
    $message =~ s/^\s+//;
    $message =~ s/\s+/ /g;
    $message =~ s/\s+$//;


    PS this does not remove non-breaking spaces (ASCII code \xA0)
    __END__

    I love Perl; it's the only language where you can bless your thingy.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    8
    Rep Power
    0
    Thank you very much for the help.
    Could you please tell me what each line is doing?
    I really want to learn this.

    Wow I see its going to take me a while to learn how to do this stuff.

    Originally Posted by shawnhcorey
    In three steps:
    perl Code:
    $message =~ s/^\s+//;
    $message =~ s/\s+/ /g;
    $message =~ s/\s+$//;


    PS this does not remove non-breaking spaces (ASCII code \xA0)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    8
    Rep Power
    0
    Which part of these 3 steps is removing the Carriage returns and line feeds if any?
  8. #5
  9. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    79
    Rep Power
    138
    Originally Posted by LRoberts
    Thank you very much for the help.
    Could you please tell me what each line is doing?
    I really want to learn this.

    Wow I see its going to take me a while to learn how to do this stuff.
    1. $message =~ s/^\s+//; # removes leading white space

    2. $message =~ s/\s+/ /g; # replaces sequence of white space with a single space

    3. $message =~ s/\s+$//; # removes trailing white space

    See:
    http://perldoc.perl.org/perlretut.html
    http://perldoc.perl.org/perlre.html
    __END__

    I love Perl; it's the only language where you can bless your thingy.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    8
    Rep Power
    0
    Ok thanks for the help.
    In order to remove carriage returns and line feeds I did this...

    # Remove Carriage returns
    chomp $instructions;

    # Remove Line Feeds
    $message =~ s/\n//g;


    Does that look correct?

    Again thank you for the help.

    Originally Posted by shawnhcorey
    1. $message =~ s/^\s+//; # removes leading white space

    2. $message =~ s/\s+/ /g; # replaces sequence of white space with a single space

    3. $message =~ s/\s+$//; # removes trailing white space

    See:
    http://perldoc.perl.org/perlretut.html
    http://perldoc.perl.org/perlre.html
  12. #7
  13. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    79
    Rep Power
    138
    Originally Posted by LRoberts
    Ok thanks for the help.
    In order to remove carriage returns and line feeds I did this...

    # Remove Carriage returns
    chomp $instructions;

    # Remove Line Feeds
    $message =~ s/\n//g;


    Does that look correct?

    Again thank you for the help.
    chomp() removes the last occurrence of the contents of $/ from the end of a string. See http://perldoc.perl.org/functions/chomp.html

    s/\n//g removes all LF characters

    s/\s+$// removes all white space from the end of a string. White space is defined as \n \r \x20 \t \f See http://perldoc.perl.org/perlre.html and http://perldoc.perl.org/perlretut.html
    __END__

    I love Perl; it's the only language where you can bless your thingy.

IMN logo majestic logo threadwatch logo seochat tools logo