Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strange result when cutting a string

Status
Not open for further replies.

laurentiuz

Programmer
Oct 26, 2002
14
0
0
FR
Hi ,

assuming the script :
24 |OLK 7204|* OLK 7204 : olkserv /port:7210
I have stored this string in a variable zltest.
If I am doing echo $zltest | awk '{ FS="|" ; print $3 }'
I will the corect result which is :
* OLK 7204 : olkserv /port:7210
but if I am doing : zltest1=`echo $zltest | awk '{ FS="|" ; print $3 }' ` and then echo $zltest1 the result is quite different (and incorect). It is as it takes the list of files which are in the curr subdir.
I've tryed with cut and with ther separators but the result is the same.

Can you let me know if I have an error somewhere or if there is another way to have the result ?

Thank you !
 
try echo "${zltest1}"

there is a '*' in the string which the shell expands to the list of files in the current dir. If you "hide" that special character from the shell then you'll get what you want.

You escape or hide special characters from the shell by preceding it by a backslash: \*, putting it in quotes '*' or "*". In this case you need double quotes because you want the shell to evaluate the variable, and the '*' is in the value of the variable.

I generally put braces around a variable name but is is not really necessary if whitespace follows the variable name.

HTH,

p5wizard
 
Hi,

The strange result is the interpretation of the meta-character "*" by your shell.

X="*"
print $X will print everything in your directory like ls * does.

2 solutions to your problem :

protect the star by a back slash this way :
zltest1=$(print $zltest | awk '{ FS="|" ; print $3 }'|sed -e 's/\*/\\*/')

translate the star character to an exotic one before affectation to your var and translate it back again later. The target character must not be in the string before:

eg :translate * to !star!

zltest1=$(print $zltest | awk '{ FS="|" ; print $3 }'|sed -e 's/\*/!star!/g')

ORIGINAL=$(print $zltest1 |sed -e 's/!star!/\*/g')

 
Thank you p5wizard & thank you aau.SOrry , Ii didn't realize this expand. I will protect the special caracters.
Regards,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top