Operating System - HP-UX
1821061 Members
2834 Online
109631 Solutions
New Discussion юеВ

Re: A simple shell question(about variable)!

 
SOLVED
Go to solution
Fragon
Trusted Contributor

A simple shell question(about variable)!

I define a variable "ssuinfo" in the shell script.
If get strings from an application. Sometimes it get "jack", sometimes it get "@jack", and also can get "@@ja@ck". The number and position of the character "@" is unsure.
But now all "@" is useless for me. So I want to truncate all "@" in the variable "ssuinfo".
How can I achieve it?

Thanks a lot!
4 REPLIES 4
Tom Geudens
Honored Contributor
Solution

Re: A simple shell question(about variable)!

Hi,
Something like this ?

export ssuinfo="@@j@ack"
echo $ssuinfo
@@j@ack
export ssuinfo=$(echo $ssuinfo | tr -d "@")
echo $ssuinfo
jack

Regards,
Tom
A life ? Cool ! Where can I download one of those from ?
Yogeeraj_1
Honored Contributor

Re: A simple shell question(about variable)!

hi,

tr -d "@"

should do it.

See extract from man tr below:
===============================================
tr copies the standard input to the standard output with substitution
or deletion of selected characters. Input characters from string1 are
replaced with the corresponding characters in string2. If necessary,
string1 and string2 can be quoted to avoid pattern matching by the
shell.

tr recognizes the following command line options:

-A Translates on a byte-by-byte basis. When this flag
is specified tr does not support extended
characters.

-c Complements the set of characters in string1,
which is the set of all characters in the current
character set, as defined by the current setting
of LC_CTYPE, except for those actually specified
in the string1 argument. These characters are
placed in the array in ascending collation
sequence, as defined by the current setting of
LC_COLLATE.

-d Deletes all occurrences of input characters or
collating elements found in the array specified in
string1.

If -c and -d are both specified, all characters
except those specified by string1 are deleted. The
contents of string2 are ignored, unless -s is also
specified. Note, however, that the same string
cannot be used for both the -d and the -s flags;
when both flags are specified, both string1 (used
for deletion) and string2 (used for squeezing) are
required.

If -d is not specified, each input character or
collating element found in the array specified by
string1 is replaced by the character or collating
element in the same relative position specified by
string2.

-s Replaces any character specified in string1 that
occurs as a string of two or more repeating
characters as a single instance of the character
in string2.

If the string2 contains a character class, the
argument's array contains all of the characters in
that character class. For example:
In a case conversion, however, the string2 array
contains only those characters defined as the
second characters in each of the toupper or
tolower character pairs, as appropriate. For
example:

tr -s '[:upper:]' '[:lower:]'

===============================================

Hope this helps!
Best Regards
Yogeeraj

No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Leif Halvarsson_2
Honored Contributor

Re: A simple shell question(about variable)!

Hi
How do you get the string into "ssuinfo" ? Something like:
ssuinfo=``
Try:
ssuinfo=` |tr -d "@"`

A. Clay Stephenson
Acclaimed Contributor

Re: A simple shell question(about variable)!

While piping the output through tr -d will ceratinly fix the problem (as long as you only get extraneous @'s), the better answer is to fix the underlying problem. You really should determine why you are getting the extraneous characters and thus fix the problem rather than dealing with the symptoms.
If it ain't broke, I can fix that.