Passing expression from a shell 
Author Message
 Passing expression from a shell

Hi,

Is it possible to implement something like this with ksh/bash and awk

#!/bin/ksh

SEARCH_CRIT="( \$0 ~ /foo/ ) || ( \$2 ~ /bar/ )"

cat somefile | awk -v on_search_crit_verif="{$SEARH_CRIT}" '

on_search_crit_verif {
        print "this line match["$0"]";

Quote:
}'

As you can imagine this way failed !!

The goal is to pass complexe criteria to awk, initialized externally
eg passed from an other script or program !

Thanks for help



Mon, 18 Apr 2005 00:30:59 GMT  
 Passing expression from a shell

% SEARCH_CRIT="( \$0 ~ /foo/ ) || ( \$2 ~ /bar/ )"
%
% cat somefile | awk -v on_search_crit_verif="{$SEARH_CRIT}" '
%
% on_search_crit_verif {
%         print "this line match["$0"]";
% }'

To do this, you need to use clever quoting to have the
shell expand the expression in your awk script. One option is to
use -f - and a HERE document to store the awk script:

  awk -f - somefile <<HERE
  $SEARCH_CRIT { print  "this line matches [" \$0 "]" }

with this approach, you need to escape $ that should be passed to
awk, but you don't need to escape ". You can also put the entire awk
script in double-quotes, in which case you need to escape both $ and ".

  awk "
    $SEARCH_CRIT { print \"this line matches [\" \$0 \"]\" }
  " somefile

Another approach is to use single-quotes for most of the script, but
double-quotes around the part where you want your shell variable expanded.

  awk '
    '"$SEARCH_CRIT"' { print "this line matches [" $0 "]" }
  ' somefile

the benefit here is that you don't have to escape anything in your
awk script, but you have to keep track of which quotes are being used
by the shell at any point in the script, and that can get hairy.

The reason -v doesn't work in this circumstance is that you are
passing an entire expression, including variable and operator names.
If you don't need that much flexibility, you might try to avoid it.

For instance, if sometimes you need the comparison above, but other
times you need to check $0 and $3, say, you could create separate
awk script files which set flags based on those differing criteria.
For instance:

  #twocomp.awk
  { matchflag = ($0 ~ foo) || ($2 ~ bar ) }

  #threecomp.awk
  { matchflag = ($0 ~ foo) && ($3 ~ bar ) }

  #mainscript.awk
  matchflag { print "this line matches [" $0 "]" }

which would be invoked like this:
  awk -v foo="${FOO}" -v bar = "${BAR}" -f twocomp.awk -f mainscript.awk
or
  awk -v foo="${FOO}" -v bar = "${BAR}" -f threecomp.awk -f mainscript.awk

--

Patrick TJ McPhee
East York  Canada



Mon, 18 Apr 2005 00:54:42 GMT  
 Passing expression from a shell


[lots of good stuff, but with one little mistake]

% shell expand the expression in your awk script. One option is to
% use -f - and a HERE document to store the awk script:
%
%   awk -f - somefile <<HERE
%   $SEARCH_CRIT { print  "this line matches [" \$0 "]" }
    HERE

(forgot to end the HERE document)

--

Patrick TJ McPhee
East York  Canada



Tue, 19 Apr 2005 02:26:44 GMT  
 Passing expression from a shell
Hi

Many thanks to you Patrick, I think i'm on the right way now

regards

Ioda



Wed, 20 Apr 2005 17:53:25 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Shell pattern to regular expression code

2. Passing shell variables to nawk snipet

3. Passing command line variables to AWK in shell script

4. passing parameter from awk back to korn shell

5. passing shell vars to gawk

6. How to pass shell variable to awk?

7. pass a variable from awk to shell

8. passing vars to shell script

9. passing shell variable into awk program

10. how do i pass shell variables into awk?

11. passing shell variable containing many fields to awk

12. Passing Shell Variables to Awk

 

 
Powered by phpBB® Forum Software