Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

how to get rid of the empty string or unprintable characters?

SOLVED
Go to Solution
Super Advisor

how to get rid of the empty string or unprintable characters?

[ Edited ]

I have a string $xyz, and the valume of that is "HP-UX". I believe there are some either null strings or unprintable characters, how do I get rid of these null strings or unpritable characters?

Thanks,

none
8 REPLIES
Honored Contributor

Re: how to get rid of the empty string or unprintable characters?

unset $xyz

or

xyz=""

live free or die
harry
Live Free or Die
Super Advisor

Re: how to get rid of the empty string or unprintable characters?

harry,

You misunderstood my question.

The xyz variable includes the value of "HP-UX", and also some null or unpritable characters, I want to get rid of these characters, and just leave exactly "HP-UX".
none
Honored Contributor

Re: how to get rid of the empty string or unprintable characters?

# xyz="HP-UX^A "
# echo $xyz | od -bc
0000000 H P - U X 001 \n
110 120 055 125 130 001 012
0000007
# xyz=`echo $xyz|tr -cd "A-Za-z-"`
# echo $xyz | od -bc
0000000 H P - U X \n
110 120 055 125 130 012
0000006
#

live free or die
harry
Live Free or Die
Honored Contributor

Re: how to get rid of the empty string or unprintable characters?

Hi,

You could do something like this

xyz=$(echo $xyz |strings|sed 's/[ ]*//g')

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Respected Contributor

Re: how to get rid of the empty string or unprintable characters?

You may try the "tr" command:

newvar=`echo $var | tr -cd "-a-zA-Z0-9"`

this deletes all characters in the strings except the ones you specify. See the manpage to understand the options.
if this makes any sense to you, you have a BIG problem
Honored Contributor

Re: how to get rid of the empty string or unprintable characters?

When you know, you only want HPUX, why not use cut command

echo $var|cut -c1-4

Anil
There is no substitute to HARDWORK
Honored Contributor

Re: how to get rid of the empty string or unprintable characters?

Hai,

We can remove the emtry stirngs using [:blank:] class and unprintable stirngs by [^:print:] (negatation of printable characters) To know more about this go to regexp man page on character class

xyz="HP-UX"

new_xyz=$(echo $xyz | tr -d "[:blank:][^:print:]")

There is a change in the old tr setup as

var="HP-UX"
new=`echo $var | tr -cd "\-a-zA-Z0-9"`

Use delimiter,because it will take as an option of tr.

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!
Highlighted
Honored Contributor

Re: how to get rid of the empty string or unprintable characters?

Hai.

To remove the control char's as like \n we have to use [[:cntrl:]] regexp class. We can remove it easily as

xy="HP-UX^X"

# echo $xyz | tr -d "[[:blank:]][[^:print:]][[:cntrl:]]"| od -bc
0000000 H P - U X A
110 120 055 125 130 101
0000006

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!