Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I strip trailing spaces from a variable? 1

Status
Not open for further replies.

maalim

Programmer
Jul 13, 2005
2
US
I am accepting values for a variable outside of the script into an input field with fixed length. Since the value could have any length (max the input field length), I need a way of stripping trailing spaces if any, within the unix script.
Here's how I pass the value from the form to the script:
filen=`echo ${1}`

Any ideas?
Thanks.
 

That will do it! [2thumbsup]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
That will do it, but it's overly complicated. This would be better...
Code:
filen=${1}
That transfers your value and removes any trailing spaces.

Hope this helps.
 
I should have added this earlier:
the value passed to the script is uppercase. I need to convert it to lowercase and append the directory in which the file is located: Example
filen = ${1} results in PR197-123.CSV. I need it to read 'pr197-123.csv. Additionally, the file is to fetched from directory /xfer/interface and passed to sqlloader for instance; it should read '/xfer/interface/pr197-123.csv'.
How do I achieve this?

Thanks.
 
And what about something like this ?
typeset -l filen=/xfer/interface/$1

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

SamBones: Your solution does not work!
Code:
$ cat r0
#!/bin/ksh
var1=`echo ${1}`
var2=$1
echo "\$1=[$1]"
echo "\$var1=[$var1]"
echo "\$var2=[$var2]"
$ r0 "Foo   "
$1=[Foo   ]
$var1=[Foo]
$var2=[Foo   ]
$
[nosmiley]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
PHV, Your solution does not remove trailing spaces either:
Code:
$ cat r0
#!/bin/ksh
typeset -l filen=/xfer/interface/$1 
echo "filen=[$filen]"
$ r0 "FOO-BAR.CSV   "
filen=[/xfer/interface/foo-bar.csv   ]
[thumbsdown]

How about:
Code:
filen="/xfer/interface/`echo ${1}|tr [:upper:][:lower:]`
[thumbsup2]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
LKBrwnDBA, my preference would be:
typeset -l filen=/xfer/interface/$(echo $1)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

Yeah -- thats better! [2thumbsup]




----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top