Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

trim whitespace?

Shell Programming and Scripting


Tags
awk, awk trim, trim, trim awk

Closed    
 
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: 22 January 2016, 4:39 PM EST
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: 8 December 2016, 4:09 PM EST
Location: Foxborough, MA
Posts: 7,989
Thanks: 195
Thanked 699 Times in 655 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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:02 PM
How to match (whitespace digits whitespace) sequence? shahanali Shell Programming and Scripting 3 09-19-2010 10:55 PM
Delete whitespace truck7758 Shell Programming and Scripting 12 12-05-2007 12:00 PM
Trim whitespace and add line break moose1 Shell Programming and Scripting 7 01-22-2007 01:53 PM



All times are GMT -4. The time now is 11:20 AM.