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 can I drop trailing spaces?

Status
Not open for further replies.

pjb

Programmer
May 1, 2001
148
US
I have an input record with a variable number of trailing spaces after the last alpha character. How can I drop these trailing blanks and shorten up the record?
 
Hi,

RTRIM is what you are looking for. This is what Oracle Documentation says -

Purpose
RTRIM returns char, with all the rightmost characters that appear in set removed; set defaults to a single blank. If char is a character literal, you must enclose it in single quotes. RTRIM works similarly to LTRIM.

Example
SELECT RTRIM('BROWNINGyxXxy','xy') "RTRIM e.g."
FROM DUAL;

RTRIM e.g
-------------
BROWNINGyxX

Hope this helps.
 
Really sorry, I just did n't realize this was the UNIX forum and not the Oracle Forum. Anyway, here's one way -

# NUM="abc "
# echo ${NUM}trail
abc trail
# NEW=`echo $NUM | sed 's/ *$//'`
# echo ${NEW}trail
abctrail

Hope THIS helps.

 
using awk:

# ------------------------------------------------------------------------
#
# Function to "trim" leading and trailing spaces from a string NOT
# disturbing the "embedded" spaces.
#
function trim(str)
{
# nsub1=gsub("^[[:space:]]+|[[:space:]]+$", "", $i);
nsub1=sub("^[ ]*", "", str);
nsub2=sub("[ ]*$", "", str);
return str;
}
# ------------------------------------------------------------------------
 
Thanks,

I found that the echo command will aslo drop the trailing spaces. Is there a problem with doing that?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top