Operating System - HP-UX
1827516 Members
2659 Online
109965 Solutions
New Discussion

RE match pattern used in grep command

 
SOLVED
Go to solution
Jdamian
Respected Contributor

RE match pattern used in grep command

Hi.

A script provided by my HP Account engineer requested me the system handle of my box. But the following error messages was repoted:

Unprintable character in 'MYSYSTEM-9000'.

I debbuged that script and found the lines that check my input:

if [ "`echo $user_info | grep '^[ !-~]*$'`" = "" ]
then
echo ""
echo "Unprintable character in '$user_info'."
continue
fi

The problem was the grep command. The following command line prints nothing:

echo hello | grep '^[ !-~]*$'

but the following alternate command line works fine:

echo hello | LANG= grep '^[ !-~]*$'
hello

I notice that grep command runs fine or not according to NLS environment (my usual LANG value is es_ES.iso88591) but what is NLS-dependant on that match pattern [ !-~] ?

According to ASCII table [ !-~] should be equivalent to [ -~] because "space" char and "!" are consecutive. This range includes all printable 7-bit ASCII characters. Therefore no problems related to NLS should be appear, shouldn't it ?
5 REPLIES 5
Elmar P. Kolkman
Honored Contributor

Re: RE match pattern used in grep command

I am not that experienced with the NLS settings, but I think you also need to check if it is related to the tilde ('~') character...
Just try different patterns for your grep RE to see if it is indeed the ' !' that is causing the problem or the '~' character...
Every problem has at least one solution. Only some solutions are harder to find.
Artyom Voronchihin
Respected Contributor

Re: RE match pattern used in grep command

Hello !
Not clear understand what are you trying to do. The first of your command (echo hello | grep '^[ !-~]*$') output nothing because where are no any of ^[ !-~]*$ characters in the world "hello". So no matches. The second command you printed assigns string value to $LANG variable. I.e.
# echo hello | LANG=grep'^[ !-~]*$'
output nothing
# echo $LANG
grep^[ !-~]*$
"Intel inside" is not a label, it's a warning.
john korterman
Honored Contributor

Re: RE match pattern used in grep command

Hi,
if grep does not find any of the strange characters it looks for, the if sentence will be true:
if [ "" = "" ]
under which circumstances it says that there is an unprintable character.
Is there not the slightest possibility that a not operator is missing, e.g.:
if [ "`echo $user_info | grep '^[ !-~]*$'`" != "" ]

Just wondering...

regards,
John K.
it would be nice if you always got a second chance
Jdamian
Respected Contributor

Re: RE match pattern used in grep command

Hi Artyom. I see you don't understand my question.

1) the match pattern [ !-~ ] means to match any character that is:
a space (ASCII code 32)
or
any char between ! (ASCII code 31) and ~ (ASCII code 126)... this means any ASCII char.

2) There is a space char between the sign = and 'grep' word, then I'm assigning nothing to LANG variable.

I checked LC_COLLATE section in file /usr/lib/nls/loc/src/es_ES.iso885915.src
and found a strange order. Meanwhile order found in C.src is identical to ASCII char, order set in es_ES.iso88591.src doesn't look to be right (perhaps).

I tried another tests:

$ echo hello | LANG= grep '^["-z]*$'
hello
$ echo hello | LANG=es_ES.iso88591 grep '^["-z]*$'
grep: Rango no válido en expresión [].

The error messages appears in Spanish.
The translation is:
grep: Invalid range within [] expression.

The same behaviour is found when other values (as it_IT.roman8) for LANG are us
Elmar P. Kolkman
Honored Contributor
Solution

Re: RE match pattern used in grep command

It is solvable by using this pattern:
grep '^[\037-\0176]*$'
Every problem has at least one solution. Only some solutions are harder to find.