The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Of bash and whitespace... lev_lafayette Shell Programming and Scripting 2 04-13-2008 09:44 PM
echo seems to trim whitespace joeyg Shell Programming and Scripting 2 02-28-2008 04:41 PM
Delete whitespace truck7758 Shell Programming and Scripting 12 12-05-2007 12:00 PM
sed : remove whitespace b.hamilton Shell Programming and Scripting 3 11-06-2007 11:02 AM
Trim whitespace and add line break moose1 Shell Programming and Scripting 7 01-22-2007 01:53 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-07-2005
msteudel msteudel is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 2
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
  #2 (permalink)  
Old 07-07-2005
msteudel msteudel is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 2
Oh and stripping out returns, line breaks etc would be nice too.
  #3 (permalink)  
Old 07-07-2005
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
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 (permalink)  
Old 07-07-2005
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2004
Location: USA
Posts: 511

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

OR


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

  #5 (permalink)  
Old 07-07-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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
Closed Thread

Bookmarks

Tags
awk, awk trim, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0