Operating System - HP-UX
1753259 Members
6054 Online
108792 Solutions
New Discussion юеВ

how to remove some null or unprintable characters withing a string

 
SOLVED
Go to solution
Hanry Zhou
Super Advisor

how to remove some null or unprintable characters withing a string

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".

HOW?
none
4 REPLIES 4
Dave La Mar
Honored Contributor

Re: how to remove some null or unprintable characters withing a string

Little more info please.
1. Do you know all the null/unprintables you want to eliminate?
2. Is there a field separator involved with "HP-UX"?

Many ways to do this dependent upon #1, such as sed, tr, awk, etc.

dl
"I'm not dumb. I just have a command of thoroughly useless information."
curt larson_1
Honored Contributor

Re: how to remove some null or unprintable characters withing a string

use the strings command

print "$var" | strings
Hein van den Heuvel
Honored Contributor
Solution

Re: how to remove some null or unprintable characters withing a string



You may want to use 'tr'. Check out 'man tr'
Notably:
-d to delete character
-c to specify the complement character range

by combining you deleted everthing except the characters listed. For example:

$ export test="-test .,;HP-UX% -"
$ echo $test | tr -cd "\-[:upper:]" | read xyz
$ echo $xyz
-HP-UX-
$ echo $test | tr -cd "\-[:upper:][:lower:]" | read xyz
$ echo $xyz
-testHP-UX-


Hein
Michael D'Aulerio
Regular Advisor

Re: how to remove some null or unprintable characters withing a string

Before you edit the string you should find out which unprintable characters are in there. Try:
echo "${xyz}" | cat -vte

The -vte options for cat are:
-v show non-printable characters as '^' followed by a character. i.e. the NULL character shows up as '^@'
-t show tabs as '^I'
-e show end-of-line as '$'
Email: michael.n.daulerio@lmco.com