Operating System - HP-UX
1824635 Members
4141 Online
109672 Solutions
New Discussion юеВ

Need to remove trailing spaces

 
SOLVED
Go to solution
Adam Noble
Super Advisor

Need to remove trailing spaces

Hi,

I am just trying to set a variable to be the model type of the system using the following:-

MODEL=`model | awk -F "[/]" '{print $3}'`

The problem I have is that on certain systems such as an RP4440 that actually produces a variable with 2 trailing spaces after it. I can't really use cut as the model strings vary. How can I simply remove the trailing spaces.

Thanks
12 REPLIES 12
Pete Randall
Outstanding Contributor

Re: Need to remove trailing spaces

From "Handy one-liners for SED" (attached):

# delete trailing whitespace (spaces, tabs) from end of each line
sed 's/[ \t]*$//' # '\t'=tab (see note at end of file)


Pete

Pete
Ivan Krastev
Honored Contributor

Re: Need to remove trailing spaces

Same in awk:

# delete leading whitespace (spaces, tabs) from front of each line
# aligns all text flush left
awk '{sub(/^[ \t]+/, ""); print}'

# delete trailing whitespace (spaces, tabs) from end of each line
awk '{sub(/[ \t]+$/, "");print}'

# delete BOTH leading and trailing whitespace from each line
awk '{gsub(/^[ \t]+|[ \t]+$/,"");print}'
awk '{$1=$1;print}' # also removes extra space between fields


regards,
ivan
James R. Ferguson
Acclaimed Contributor
Solution

Re: Need to remove trailing spaces

Hi Adam:

# MODEL=`model | awk -F "/" '{gsub(/ /,"");print $3}'`

Regards!

..JRF...
Adam Noble
Super Advisor

Re: Need to remove trailing spaces

Thanks All much appreciated.
Arturo Galbiati
Esteemed Contributor

Re: Need to remove trailing spaces

Hi Adam,
it's strange. I run same command and the result on my HPux11i it's fine:
%MODEL=`model | awk -F "[/]" '{print $3}'`
%echo "#$MODEL#"
#rp7420#

I also simulate the trouble using a var with spaces at the end but the result it's fine as well:

%x="9000/800/rp7420 "
%echo "#$x#"
#9000/800/rp7420 #
%MODEL=`echo $x | awk -F "[/]" '{print $3}'`
%echo "#$MODEL#"
#rp7420#

HTH,
Art
Dennis Handly
Acclaimed Contributor

Re: Need to remove trailing spaces

>Art: I run same command and the result on my 11i it's fine:

Send the output of model to a file and see if trailing spaces.

>Art: I also simulate the trouble using a var with spaces at the end but the result is fine as well:
MODEL=`echo $x | awk -F "[/]" '{print $3}'`

This isn't valid simulation. If you change to the following to quote the variable $x, you will preserve the spaces:
MODEL=$(echo "$x" | awk -F "[/]" '{print $3}'); echo "$MODEL x"

Also the scummy C shell seems to strip the trailing spaces because the result of `` has trailing whitespaces removed. But not a real shell.
Adam Noble
Super Advisor

Re: Need to remove trailing spaces

yes now you say this I log onto other RP4440 servers and there is not a problem however I'll send you the output below to prove my point. Can't think what it is then:-

/root]MODEL=`model | awk -F "[/]" '{print $3}'`
root:/root]echo $MODEL
rp4440
root@meskpweb:/root]echo "$MODEL adam"
rp4440 adam

Peter Nikitka
Honored Contributor

Re: Need to remove trailing spaces

Hi Adam,

your space is located between the two string in the string presented to the echo command: "$MODEL adam"

root@meskpweb:/root]echo "$MODEL adam"
rp4440 adam

try
echo ":$MODEL:"

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Adam Noble
Super Advisor

Re: Need to remove trailing spaces

No I've lost the plot I printed the valid system output this is what I should have printed which is from the server I have the issue with:-

root@:/root]MODEL=`model | awk -F "[/]" '{print $3}'`
root@:/root]echo $MODEL
rp4440
root@meskpweb:/root]echo "$MODEL adam"
rp4440 adam
root@:/root]

Adam Noble
Super Advisor

Re: Need to remove trailing spaces

I'm going crackers here!!!! The space is visible when I copy it into the reply box. However when I submit it the spaces dissapear. It looks like this "rp4440 adam" however maybe this space will dissapear

O well trust me its there on the unix box as when I create my output all servers are fine apart from this one which has two spaces before the next variable. Anyway the problem is fixed and I am going mad.

Thanks
Hein van den Heuvel
Honored Contributor

Re: Need to remove trailing spaces

>> I'm going crackers here!!!! The space is visible when I copy it into the reply box.

The forum webpage/software is nasty that way.

Multiple tabs/spaces are condended to 1 space UNLESS you check the box for:
"Retain format(spacing). URLs will not be clickable"

When discussing a problem where exact input or output format is critical it is best to attach a ".txt" file with clear examples of what is shown and how it should be transformed.

fwiw,
Hein.
Adam Noble
Super Advisor

Re: Need to remove trailing spaces

thanks for that so I'm not going mad which is good.