Re: how to get rid of the empty string or unprintable characters? (238 Views)
Reply
Super Advisor
Hanry Zhou
Posts: 1,167
Registered: ‎01-11-2002
Message 1 of 9 (238 Views)
Accepted Solution

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
Please use plain text.
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 2 of 9 (238 Views)

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
Please use plain text.
Super Advisor
Hanry Zhou
Posts: 1,167
Registered: ‎01-11-2002
Message 3 of 9 (238 Views)

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
Please use plain text.
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 4 of 9 (238 Views)

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
Please use plain text.
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 5 of 9 (238 Views)

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
Please use plain text.
Respected Contributor
Heiner E. Lennackers
Posts: 339
Registered: ‎02-24-2002
Message 6 of 9 (238 Views)

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
Please use plain text.
Honored Contributor
RAC_1
Posts: 5,920
Registered: ‎03-21-2002
Message 7 of 9 (238 Views)

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
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 8 of 9 (238 Views)

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!
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 9 of 9 (238 Views)

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!
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation