LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Regex in Linux does not work (https://www.linuxquestions.org/questions/linux-newbie-8/regex-in-linux-does-not-work-4175558450/)

frodobag 11-09-2015 10:00 PM

Regex in Linux does not work
 
Hi All,

Here's the script I was testing. In Linux my shell enviroment is bash.
The objective is to test if my input is a whole number like 1, or 52 or 1000 and running the script it will not say anything as expected. Otherwise for any other input that doesn't match the criteria it will say "error: Not a number" and quit.

#!/bin/bash
re='^[0-9]+$'
printf "`echo -n Enter a number or anuthing to test:` \n"
read char
if ! [[ "$char" =~ "$re" ]]; then
echo "error: Not a number !!!" >&2; exit 1
fi

FYI the above script works fine in one Linux pc in a bash shell.

But the same script when used in another Linux pc which uses Bourne shell (sh) - it does not work :(
All the whole numbers and everything else it gives the error message. Can somebody please help shed some light ?

thanks,
frodobag

frankbell 11-09-2015 10:16 PM

What distro is on that other PC?

frodobag 11-09-2015 10:17 PM

Ubuntu

berndbausch 11-09-2015 10:35 PM

Quote:

Originally Posted by frodobag (Post 5447301)
FYI the above script works fine in one Linux pc in a bash shell.

But the same script when used in another Linux pc which uses Bourne shell (sh) - it does not work :(
All the whole numbers and everything else it gives the error message. Can somebody please help shed some light ?

Bash is an extension of the Bourne shell. This means that Bash understands Bourne shell syntax, but has syntax elements that the Bourne shell doesn't understand.

In particular, I think the [[ ... ]] construct doesn't exist in the Bourne shell.

What is the error message?

EDIT: Your program doesn't work because of the following (from the bash reference guide):

Quote:

If the pattern is stored in a shell variable, quoting the variable expansion forces the entire pattern to be matched as a string
That is, since you put quotes around $re, you are testing "if $char matches the string $re", not the intended "if $char matches the regular expression $re".
If char has a value of, say, 'fdg^[0-9]+$lk', the expression [[ $char =~ "$re" ]] will be true.

Thus, to check if char is a number, remove the quotes around $re. You can also remove the quotes around $char, since they are not needed inside [[ ... ]].

frodobag 11-09-2015 10:48 PM

no error message. Just that after I run the script and when I type in 1, 52, or 1000 it erroneously outputs "error: Not a number !!" instead of outputting nothing and quietly exiting as would be expected.

berndbausch 11-09-2015 10:53 PM

Quote:

Originally Posted by frodobag (Post 5447311)
no error message. Just that after I run the script and when I type in 1, 52, or 1000 it erroneously outputs "error: Not a number !!" instead of outputting nothing and quietly exiting as would be expected.

See my updated post above. For giggles, try entering fdg^[0-9]+$lk and see what happens.

frodobag 11-09-2015 11:07 PM

Nope that didn't work. But oddly enough I was trying some variations and with re='^[0-9]+$' and now it works! But thanks all, at least it jogs my thoughts a bit.

frodobag 11-09-2015 11:25 PM

A side note: I am using the above test script for a more complex script to read a log file. Now since when I type in whole numbers on the keyboard , I guess the regex recognise it as actually numbers so it is correct. But when I extract a value with my more complex script from the log file using grep , cut ,sed, that value , although I see it as a number but is it possible the regex comparison I use above, "sees" it as a text ? and maybe thats why it says "not a number" ?

syg00 11-10-2015 12:54 AM

I don't use Bourne shell, but the bash abs guide notes that both the [[ ... ]] extended test and regex match aren't supported in Bourne and are portability issues.

chrism01 11-10-2015 01:08 AM

Yeah, the original sh shell is less capable than bash (hence the name ;) ).
I'd stick with the latter, unless of course you want to move up to eg Perl (which is red hot on regexes...)

berndbausch 11-10-2015 01:21 AM

Quote:

Originally Posted by frodobag (Post 5447323)
A side note: I am using the above test script for a more complex script to read a log file. Now since when I type in whole numbers on the keyboard , I guess the regex recognise it as actually numbers so it is correct. But when I extract a value with my more complex script from the log file using grep , cut ,sed, that value , although I see it as a number but is it possible the regex comparison I use above, "sees" it as a text ? and maybe thats why it says "not a number" ?

The =~ operator matches text. It has no notion of numbers.

Your bash fragment above says "not a number" because when your $re is surrounded by quotes, you match $char against a mere string, not a regular expression. Since 12345 doesn't contain the string ^[0-9]+$, the test fails. When you remove the quotes, $re is interpreted as a regexp and the test succeeds.

You say "nope it doesn't work", but I wonder what it is that doesn't work? I am curious to see your code, your input and your output.

Edit: I participate here in parts because I learn. I didn't know about these details of =~ and would like to gain an even deeper understanding. I don't insist for insistance's sake.

pan64 11-10-2015 01:39 AM

looks like you need to use:
Code:

# instead of "$re"
if ! [[ "$char" =~ $re ]]; then


chrism01 11-10-2015 04:39 PM

There's a couple of good explanations/HOWTOs here
http://www.tldp.org/LDP/abs/html/regexp.html
http://www.itworld.com/article/26933...pressions.html - this one has an example of your problem :)

frodobag 11-10-2015 07:21 PM

Thanks guys for the hints.

Here's a snippet of the other script:

#!/bin/bash
#re='^\d(\d)?(\d)?(\d)?(\d)?$ '
re='^[0-9]+$'
char=` ...just grepping some whole numbers from a log file here, like 1234 or 56, etc...`
if ! [[ $char =~ $re ]] ; then
echo "error: Not a number !!!"
else
echo " Whole number - good"
fi

I've tried... if ! [[ "$char" =~ $re ]] ; then
...as well...along with other regex but the output was "error: not a number" even when the char value was something like 1234, when I expect it to say " Whole number - good" instead. Only the char value is let's say a text like THISTEXT or with special characters like 1234-456:7:8 then it should say "error: Not a number". But as of now all these 3 examples it says " error" ,which at this point still doesn't work.

I probably need to find an alternative to the =~ operator and [[..]]

frodobag 11-10-2015 08:46 PM

I think I should elaborate char=` ...just grepping some whole numbers from a log file here, like 1234 or 56, etc...`
The way I use to grep might be the problem.

I use... tac logfile | grep "(1.)" |grep -E '[0-9]{1,4}' | head -1

So the line in the logfile gets selected for example.... (1.) This is a line 1234 and that's it. Date: 12-12-2015
So it will pick out 1234 correctly but still, with the extra grep -E command, it doesn't help. While the result of grep of 1234 is correct, the result of the comparison operator +~ is not, which always say "error:Not a number".


All times are GMT -5. The time now is 05:24 AM.