awk question about matching a pattern 
Author Message
 awk question about matching a pattern

I have an awk question that I hope someone can help me with.

In a korn shell script I am going through a file that has all the
oracle databases defined in it.  As part of the data in the line
($LINE), there is the oracle version which I want have contained in an
environment variable ($ORACLE_VERSION).  When I run my script the only
value that gets set is 901 even when the line has 817 and not the
pattern 901.  Can someone tell me how to change the below lines such
that it will work within a korn script on AIX or ive me an alternative
solution?

Thanks in advance.

#if the pattern is matched then the variable gets set
 ORACLE_VERSION=`echo $LINE | awk '{if ($0 ~/817/) print "817"}' `
 ORACLE_VERSION=`echo $LINE | awk '{if ($0 ~/901/) print "901"}' `
 export ORACLE_VERSION



Tue, 16 Nov 2004 08:34:16 GMT  
 awk question about matching a pattern

Quote:
> I have an awk question that I hope someone can help me with.

> In a korn shell script I am going through a file that has all the
> oracle databases defined in it.  As part of the data in the line
> ($LINE), there is the oracle version which I want have contained in an
> environment variable ($ORACLE_VERSION).  When I run my script the only
> value that gets set is 901 even when the line has 817 and not the
> pattern 901.  Can someone tell me how to change the below lines such
> that it will work within a korn script on AIX or ive me an alternative
> solution?

> Thanks in advance.

> #if the pattern is matched then the variable gets set
>  ORACLE_VERSION=`echo $LINE | awk '{if ($0 ~/817/) print "817"}' `
>  ORACLE_VERSION=`echo $LINE | awk '{if ($0 ~/901/) print "901"}' `
>  export ORACLE_VERSION

So, in korn, what happens if awk doesn't print anything?  Is ORACLE_VERSION
set to "" or destroyed, or maybe, does the assignment not happen?  And then,
what happens at the parent level when you export a variable that is blank or
gone?  Does the parent environment change, or not?

If there were a match on 817, the (next) 901 line would blank
ORACLE_VERSION, so the parent would be left with however the export of a
null evaluation works.  If there were a match on 901, though, it seems to me
clear that 901 would be exported.  So, from the calling level, I would
expect to see ORACLE_VERSION set to blank, destroyed, or set to 901.  But
only a trick set of functionality would set it to 817 after 817 succeeds and
901 fails.

Below, I have a suggestion for setting ORACLE_VERSION with one line, rather
than two lines, of shell script.

Have you looked at awk's system() and cmd|getline functions?  They might let
you bring all of the processing into awk in a cleaner way.

Are you sure of what the contents of $LINE are?  (We can't be, because you
didn't include the shell script sets it.)  If it is not a single line from
the file with no newline, as you described, you might get unexpected
results.

Maybe the korn shell needs fancy quoting of the $0 in the awk code?  But I
don't see how this could cause your problem.

The awk presented can be simplified a bit.

Quote:
>    awk '{if ($0 ~/817/) print "817"}'

Awk's "pattern" is essentially a $0 match, so rather than
    {if ($0 ~/817/) print "817"}
you could say
    /817/ {print "817"}

You could generalize the program to handle both cases,
    ver.awk
    ----------
    {
        if (match($0,/817|901/))
            print substr($0,RSTART,RLENGTH)
        else
            print "?"
    }
Then use one line:
    ORACLE_VERSION=`echo $LINE | awk -f ver.awk`
If the value "901" is just getting left aound because "817" isn't matching
later on, this would let you know with a "?".

    - Dan



Tue, 16 Nov 2004 10:10:13 GMT  
 awk question about matching a pattern

Quote:
> I have an awk question that I hope someone can help me with.

> In a korn shell script I am going through a file that has all the
> oracle databases defined in it.  As part of the data in the line
> ($LINE),

What line?  I see no examples of your data.

Quote:
> there is the oracle version which I want have contained in an
> environment variable ($ORACLE_VERSION).  When I run my script the only
> value that gets set is 901 even when the line has 817 and not the
> pattern 901.  Can someone tell me how to change the below lines such
> that it will work within a korn script on AIX or ive me an alternative
> solution?

> Thanks in advance.

> #if the pattern is matched then the variable gets set
>  ORACLE_VERSION=`echo $LINE | awk '{if ($0 ~/817/) print "817"}' `
>  ORACLE_VERSION=`echo $LINE | awk '{if ($0 ~/901/) print "901"}' `
>  export ORACLE_VERSION

Why are you using awk for a simple shell problem?

  case $LINE in
    *817*) ORACLE_VERSION=817 ;;
    *901*) ORACLE_VERSION=901 ;;
  esac

Harlan, don't read this post.  ;)

--
David Thompson



Tue, 16 Nov 2004 14:25:27 GMT  
 awk question about matching a pattern

Quote:
> I have an awk question that I hope someone can help me with.

> In a korn shell script I am going through a file that has all the
> oracle databases defined in it.  As part of the data in the line
> ($LINE), there is the oracle version which I want have contained in an
> environment variable ($ORACLE_VERSION).  When I run my script the only
> value that gets set is 901 even when the line has 817 and not the
> pattern 901.  Can someone tell me how to change the below lines such
> that it will work within a korn script on AIX or ive me an alternative
> solution?

> Thanks in advance.

> #if the pattern is matched then the variable gets set
>  ORACLE_VERSION=`echo $LINE | awk '{if ($0 ~/817/) print "817"}' `
>  ORACLE_VERSION=`echo $LINE | awk '{if ($0 ~/901/) print "901"}' `
>  export ORACLE_VERSION

Does this do it?

#if the pattern is matched then the variable gets set
ORACLE_VERSION=`echo $LINE | awk '{if ($0 ~ /817/) print "817"
                                   else if ($0 ~/901/) print "901"}' `
export ORACLE_VERSION

HTH
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Tue, 16 Nov 2004 14:01:12 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. awk pattern matching?

2. how to use a shell variable in awk for pattern matching

3. Help on pattern matching in awk

4. awk -- pattern match a line and the line that follows

5. iss-matching - the free Regular Expression / Pattern Matching cluster

6. Question on Pattern Matching

7. Newbie Question Pattern match

8. Question about pattern-matching compiler.

9. syntax question, pattern matching and arity-0 constructors:

10. pattern matching - question from newbie

11. pattern matching and read tables question

12. Newbie Generic Reg Exp Pattern Matching Question

 

 
Powered by phpBB® Forum Software