Operating System - HP-UX
1752672 Members
5960 Online
108789 Solutions
New Discussion юеВ

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

 
SOLVED
Go to solution
Hanry Zhou
Super Advisor

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

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 8
harry d brown jr
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
Hanry Zhou
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
harry d brown jr
Honored Contributor
Solution

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
Sridhar Bhaskarla
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
Heiner E. Lennackers
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
RAC_1
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
Muthukumar_5
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!
Muthukumar_5
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!