The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Trailing spaces chopped off - MF to Unix FTP. videsh77 UNIX for Advanced & Expert Users 14 01-08-2009 05:06 PM
Remove Trailing spaces after a delimiter kiran_418 UNIX for Dummies Questions & Answers 1 04-29-2008 02:19 PM
How to remove trailing spaces mahek_bedi UNIX for Dummies Questions & Answers 2 08-10-2007 07:21 AM
Removing leading and trailing spaces of data between the tags in xml. jhmr7 UNIX for Dummies Questions & Answers 2 05-18-2005 10:27 AM
Leading and Trailing Spaces sleepster Shell Programming and Scripting 7 10-29-2003 11:48 PM

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

Join Date: Feb 2005
Posts: 146
Smile Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help.

echo $SH_VAR | command_line Syntax.

The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and trailing spaces to be removed. If anyone has accomplished this before or knows a quick way to achieve this result please let me know

Thnx
  #2 (permalink)  
Old 03-07-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
#!/bin/ksh

a=' abc de f    '

b=$(echo ${a} | sed -e 's/^ *//g;s/ *$//g')

echo "a->[${a}] b->[${b}]"
  #3 (permalink)  
Old 03-07-2005
ZealeS ZealeS is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 29
delete leading whitespace (spaces, tabs)from begin of each line
Code:
sed 's/^[ \t]*//'
delete trailing whitespace (spaces, tabs)from end of each line
Code:
sed 's/[ \t]*$//'
try
Code:
sed 's/^[ \t]*//;s/[ \t]*$//' yourfilename
  #4 (permalink)  
Old 03-07-2005
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
Thank you for the sed version.
the

echo $var1 | sed 's/^[ \t]*//;s/[ \t]*$//' |& read -p var2

strips the leading and trailing spaces from the variable var1 and sets the output to the new variable var2.

Thank you.
  #5 (permalink)  
Old 03-07-2005
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
I have encountered an issue in my previous command.

The command works for a variable with a single space embedded in it.
However when you have more than one space in the variable value the command strips extrac spaces and leaves only one inside of the value.

Ex: a=' abc de f '

returns b='abc de f'

however if a=' abc de f '
returns b='abc de f'

Any suggesstions to overcome this issue would be much appreciated. Thanks
  #6 (permalink)  
Old 03-07-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
We can extend vgersh99's code to handle this by adding a few double quotes...

Code:
#!/bin/ksh

a=' abc            de f    '

b=$(echo "${a}" | sed -e 's/^ *//g;s/ *$//g')

echo "a->[${a}] b->[${b}]"
  #7 (permalink)  
Old 03-07-2005
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
The double quotes did the trick. Thanks for your input.
Sponsored Links
Closed Thread

Bookmarks

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:48 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