trim whitespace? | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

trim whitespace?

Shell Programming and Scripting


Tags
awk, awk trim, trim, trim awk

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-07-2005
msteudel msteudel is offline
Registered User
 
Join Date: Jul 2005
Last Activity: 24 August 2005, 6:17 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
trim whitespace?

I'm trying to find a command that will trim the white space off a string.

e.g.
Code:
$str = "      stuf    "

$str = trim ( $str )

echo $str // ouput would just be stuf

Thanks, Mark
Sponsored Links
    #2  
Old Unix and Linux 07-07-2005
msteudel msteudel is offline
Registered User
 
Join Date: Jul 2005
Last Activity: 24 August 2005, 6:17 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Oh and stripping out returns, line breaks etc would be nice too.
Sponsored Links
    #3  
Old Unix and Linux 07-07-2005
tmarikle tmarikle is offline Forum Advisor  
Registered User
 
Join Date: Jan 2005
Last Activity: 27 September 2011, 12:36 PM EDT
Posts: 683
Thanks: 0
Thanked 4 Times in 4 Posts
Here is one way to trim spaces (KSH syntax):

Code:
$ str=$(print "$str" | nawk '{gsub(/^[ ]*/,"",$0); gsub(/[ ]*$/,"",$0) ; print }')

You can certainly expand it to eliminate other characters.
    #4  
Old Unix and Linux 07-07-2005
bhargav's Unix or Linux Image
bhargav bhargav is offline Forum Advisor  
Registered User
 
Join Date: Sep 2004
Last Activity: 26 July 2012, 3:38 PM EDT
Location: USA
Posts: 512
Thanks: 0
Thanked 2 Times in 2 Posts

Code:
echo "      stuf    " | tr -s " "

OR


Code:
echo "      stuf    " | tr -s " " | sed 's/^[ ]//g

Sponsored Links
    #5  
Old Unix and Linux 07-07-2005
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 1 April 2015, 7:21 PM EDT
Location: Foxborough, MA
Posts: 7,773
Thanks: 170
Thanked 636 Times in 594 Posts
Quote:
Originally Posted by bhargav
Code:
echo "      stuf    " | tr -s " "

OR


Code:
echo "      stuf    " | tr -s " " | sed 's/^[ ]//g

echo '[ foo ]' | tr -s ' '
[ foo ]

echo ' foo ]' | tr -s ' ' | sed 's/^[ ]//g'
foo ]

echo '[ foo bar ]' | tr -s ' '
[ foo bar ]

echo '[ foo bar ]' | tr -d ' '
[foobar]

echo ' foo bar ' | sed 's/^ *//;s/ *$//'
foo bar
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting rid of whitespace mosdojaf Shell Programming and Scripting 2 09-20-2010 10:02 PM
How to match (whitespace digits whitespace) sequence? shahanali Shell Programming and Scripting 3 09-19-2010 09:55 PM
echo seems to trim whitespace joeyg Shell Programming and Scripting 2 02-28-2008 03:41 PM
Delete whitespace truck7758 Shell Programming and Scripting 12 12-05-2007 11:00 AM
Trim whitespace and add line break moose1 Shell Programming and Scripting 7 01-22-2007 12:53 PM



All times are GMT -4. The time now is 08:09 PM.