Regular expression in conditional question

Regular expression in conditional question

Post by --==[bolMyn]==- » Sat, 27 May 2000 04:00:00



I never had to do it before but there is always the first time.  I wrote a
script in which I convert data into ASCII comma delimited file.  It's quite
long so I won't post it here unless requested.  As always is the case with
projects like this, clients always come up with the strangest ideas.  In any
case, I have to check if a field from a record contains a string of numbers
only and if it doesn't, I would replace it with a different field.

I came up with this:

case $myVariable in
    [0-9][0-9][0-9][0-9] ) # in reality there are 7 digits I have to check
for.
        ;;
    * )
        myVariable=$anotherField
        ;;
case

I have quickly come to learn that this approach will become very cumbersome
very fast.  Hence the question.  Can I use a regular expression in a
conditional like this:

if [ "$myVariable" != '[0-9][0-9][0-9][0-9]' ]; then
    something else
fi

It doesn't quite work for me so obviously there is a flaw in my thinking.
So is it possible and so, how would I do it?

Thanks.

--

-----------
Bolek,

URL: http://www.bolek.com

ICQ: 4086197
Address: 402905326

 
 
 

Regular expression in conditional question

Post by Kermit Lowry, II » Sat, 27 May 2000 04:00:00




Quote:> In any
> case, I have to check if a field from a record contains a string of
numbers
> only and if it doesn't, I would replace it with a different field.

Here is an interesting solution.
Say the file looks like this

1234567 mommy 1234
123abcd daddy 2345

try:

awk ' $1 != int($1) { $1 = $3 }; { print}' file
   would produce

1234567 mommy 1234
2345 daddy 2345

You could use cut and typeset -i ( or integer ) in a ksh script
with approximately the same result.

HTH,
-- Kermit Lowry,III
----------------
"Only you can prevent forest fires!" -Smoky
at kp.org use kermit.lowry

This input does not necessarily reflect the opinion of my employer.

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

Regular expression in conditional question

Post by --==[bolMyn]==- » Sat, 27 May 2000 04:00:00


Thanks Kermit.  I'll give it a go today. :-)





> > In any
> > case, I have to check if a field from a record contains a string of
> numbers
> > only and if it doesn't, I would replace it with a different field.

> Here is an interesting solution.
> Say the file looks like this

> 1234567 mommy 1234
> 123abcd daddy 2345

> try:

> awk ' $1 != int($1) { $1 = $3 }; { print}' file
>    would produce

> 1234567 mommy 1234
> 2345 daddy 2345

> You could use cut and typeset -i ( or integer ) in a ksh script
> with approximately the same result.

> HTH,
> -- Kermit Lowry,III
> ----------------
> "Only you can prevent forest fires!" -Smoky
> at kp.org use kermit.lowry

> This input does not necessarily reflect the opinion of my employer.

> Sent via Deja.com http://www.deja.com/
> Before you buy.

 
 
 

Regular expression in conditional question

Post by --==[bolMyn]==- » Sat, 27 May 2000 04:00:00






crash => > > In any
crash => > > case, I have to check if a field from a record contains a string of
crash => > numbers
crash => > > only and if it doesn't, I would replace it with a different field.
crash => > >
crash => > Here is an interesting solution.
crash => > Say the file looks like this
crash => >
crash => > 1234567 mommy 1234
crash => > 123abcd daddy 2345
crash => >
crash => > try:
crash => >
crash => > awk ' $1 != int($1) { $1 = $3 }; { print}' file
crash => >    would produce

O.K.  Tried it but I finally found a better solution for both, awk and
shell.  My final piece of code was this:

gawk '\
BEGIN { RS = ""; FS = "\n"; OFS= "," }
{ node = substr($6, 9,4); sknode = substr($1, 3,4)
if ( node !~ /[0-9]\{4\}/{
        if ( sknode ~ /[0-9]\{4\}/ ){
                node = sknode
        }

Quote:}

print $1, $2, node, sknode }' < myfile > anotherfile

There is another thing I've leanred from this excersise.  Unless I use
gawk --posix explicitly, it will not understand interval
quantifiers.  I'm not sure if I have an older version of gawk or not, but
I've been to "hell and back" just to find that out....  Talk about
learning new things the hard way... :-)

crash => >
crash => > 1234567 mommy 1234
crash => > 2345 daddy 2345
crash => >
crash => > You could use cut and typeset -i ( or integer ) in a ksh script
crash => > with approximately the same result.
crash => >
crash => > HTH,
crash => > -- Kermit Lowry,III
crash => > ----------------
crash => > "Only you can prevent forest fires!" -Smoky
crash => > at kp.org use kermit.lowry
crash => >
crash => > This input does not necessarily reflect the opinion of my employer.
crash => >
crash => >
crash => > Sent via Deja.com http://www.deja.com/
crash => > Before you buy.
crash =>
crash =>
crash =>

--

--------
Bolek,

URL: http://www.bolek.com

ICQ: 4086197
Address: 402905326

 
 
 

1. Conditional logging in Apache and regular expressions

Hi there,
I've been trying to set a custom log that will contain entries:
1. belonging ONLY to logged in users, when the REMOTE_USER environment
variable is set,
and
2. while omitting all jpg, gif, css files.
The second part is easy:

SetEnvIfNoCase Request_URI \.(gif|jpg|js|css)$ dontlog=1
CustomLog "|rotatelogs logs/my_log 86400" combined env=!dontlog

but the first part, well, I couldn't make it work.
In essence, I was not able to find the correct regexp indicating an
existing remote user.

Can anyone help me, please?

2. can't compile--bugs in pl70

3. Question on conditional expressions in Bourne shell acripts

4. typo in Makefile

5. regular expression question

6. A dumb question about X authorization

7. Regular Expression Question

8. ZMAGIC and QMAGIC ????????????

9. question about find - regular expressions in the path

10. A simple regular expression question

11. Grep: Regular Expression question

12. Regular expression question...

13. Regular Expression Question