|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Oh and stripping out returns, line breaks etc would be nice too.
|
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
Code:
echo " stuf " | tr -s " " OR Code:
echo " stuf " | tr -s " " | sed 's/^[ ]//g |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
Quote:
[ 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 | ||
|
![]() |
| Tags |
| awk, awk trim, trim, trim awk |
| Thread Tools | Search this Thread |
| Display Modes | |
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 |
|
|