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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check for numbers in a string 1

Status
Not open for further replies.
Feb 12, 2002
80
NO
Hi,

I am in the middle of a script and need to check if one of my variables has numbers in it.

I tried explaing why but it didn;t makes sense so hopefully just pasting my code will explain in itself:

Code:
    #FIELD5 and DC are text variables
    # check if FIELD5 is DC.1.rode, DC.2.rode etc

       if [ $FIELD5 = ${DC}.[0-9].rode ]
       then
              #do stuff
       else
              #do other stuff
       fi

I hope that makes some sense.

How do I get this to work?
There could be one or two numbers.

Thanks,
littleIdiot
 
if [ -f DC.[0-9].rode ]; then
echo file exists
else
echo "Nope"
fi



Mike

Unix *is* user friendly. It's just selective about who its friends are.
 
Hmmm - not quite what I'm trying to do ...

It's not that I'm trying to check fi the file exists (I already know it does).

I'm trying to see if the variable FIELD5 is the same as DC.#.rode, where # is a number, between 1 and 99.

see the difference?
 
Hi

Code:
[gray]# in BASH you need this before :[/gray]
shopt -s extglob
[gray]# [/gray]


if [[ $FIELD5 == DC.+([0-9]).rode ]]
then
  [gray]#do stuff[/gray]
else
  [gray]#do other stuff[/gray]
fi

Feherke.
 
In all bourne compatible shells:
case $FIELD5 in
DC.[0-9].rode) : do stuff;;
*) : do other stuff;;
esac

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Bourne solution with case and possible 2 digit number:

case $FIELD5 in
DC.[0-9].rode|DC.[0-9][0-9].rode) : do stuff;;
*) : do other stuff;;
esac

other Bourne way is (any number allowed):

number=`echo $FIELD5|cut -d. -f2`
number=`expr $number + 0 2>/dev/null`
if [ $? -eq 0 ]
then
: do stuff
else
: do other stuff
fi


HTH,

p5wizard
 
Another idea:

Code:
if echo $FIELD5 | grep -q 'DC.[0-9]*.rode'
then
    ...
fi

If your grep doesn't support -q (quiet) then you can just add a > /dev/null at the end.

Annihilannic.
 
Or...
Code:
#!/bin/ksh

# FIELD5 and DC are text variables
# check if FIELD5 is DC.1.rode, DC.2.rode etc,
# up to DC.99.rode


if [[ $FIELD5 = @(${DC}.([0-9]|[0-9][0-9]).rode) ]]
then
      #do stuff
else
      #do other stuff
fi
This will match one or two numeric characters in the middle. No more, no less.
 
Thank you for the many solutions.

for my specific case, the solution provided by feherke seems to be working fine - but it may hit a problem where there are 2 numbers (i.e. change from .9 to .10,.11 etc).

I guess if i see this problem then i will edit to use the pipe [0-9][0-9] option.

thanks for the help - i left work at 4.30pm CET today and left my script running on the 500,000 or so records I need to run this on (!).

we will see how it has performed in the morning .. .no doubt will have crashed something ....

thanks,
littleIdiot
 
Hi

littleIdiot said:
for my specific case, the solution provided by feherke seems to be working fine - but it may hit a problem where there are 2 numbers (i.e. change from .9 to .10,.11 etc).
Could you test it, please, before supposing that "may hit a problem" ?

Both [tt]bash[/tt] and [tt]ksh[/tt] :
Code:
[blue]master #[/blue] FIELD5=DC.?.rode
[blue]master #[/blue] [[ $FIELD5 == DC.+([0-9]).rode ]] && echo OK || echo NO
NO
[blue]master #[/blue] FIELD5=DC.1.rode
[blue]master #[/blue] [[ $FIELD5 == DC.+([0-9]).rode ]] && echo OK || echo NO
OK
[blue]master #[/blue] FIELD5=DC.1x.rode
[blue]master #[/blue] [[ $FIELD5 == DC.+([0-9]).rode ]] && echo OK || echo NO
NO
[blue]master #[/blue] FIELD5=DC.12.rode
[blue]master #[/blue] [[ $FIELD5 == DC.+([0-9]).rode ]] && echo OK || echo NO
OK
Yes, it is not perfect, because will accept numbers with more then 2 digits too :
Code:
[blue]master #[/blue] FIELD5=DC.2006.rode
[blue]master #[/blue] [[ $FIELD5 == DC.+([0-9]).rode ]] && echo OK || echo NO
OK
Here is another, shell independent solution :
Code:
if expr $FIELD5 : "DC.[[:digit:]]\{1,2\}.rode" > /dev/null
then
  [gray]#do stuff[/gray]
else
  [gray]#do other stuff[/gray]
fi
But I would not us it, as [tt]expr[/tt] is abit slow.

Feherke.
 
For completeness I've amended mine to match the 'only one or two digit' requirement:

Code:
if echo $FIELD5 | grep -q 'DC.[0-9][0-9]?.rode'
then
    ...
fi


Annihilannic.
 
D'oh, mixing my regexp formats. [blush]

This is the one that works:

Code:
if echo $FIELD5 | grep -q 'DC.[0-9]\{1,2\}.rode'
then
    ...
fi

Annihilannic.
 
Hi

Annihilannic, there was nothing wrong with your first regular expression with the question mark. Just needed a -E option for the [tt]grep[/tt].
Code:
echo $FIELD5 | grep [red]-E[/red] -q 'DC.[0-9][0-9]?.rode'

Feherke.
 
Yep, thanks feherke. I try and avoid egrep/grep -E for perforance reasons unless I really need extended regexp. And if possible I use fgrep/grep -F when I don't need any regexp at all...

Annihilannic.
 
Hi

Hmm... You say this would be faster just because lacks the -E option ? Interesting. I will investigate it sometime.
Code:
echo $FIELD5 | grep -q 'DC.[0-9][0-9][red]\[/red]?.rode'

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top