Hi, Is it possible to refer to a variable inside regular expression in awk ? variable=abc The following works /abc/ {print} but the following does not work /variable/ {print} ### Refering to a variable What are the alternatives ? thanks,

Index() function compares plain strings, not patterns.

The /…/ notation only works for patterns directly declared between the //. In addition, without an operator it makes a match against the whole of the current line.

If you want to check a field or a variable, you need to specify the ~ operator:

$3 ~ /foo.*bar/ OR myVar ~ /foo.*bar/

There are two ways to use a pattern that is contained in an awk string variable. (If I declare a pattern.RE in a variable, I name the variable reSomething, like:

reWord = “[1]+$”;

Then the two available syntaxes are:

(a) someVar ~ reWord

That just returns a boolean result – 1 for match, 0 for not-match

(b) match (someVar, reWord)

That returns the index of the match, or zero (which is a useful boolen too).

In addition, it sets the global variables RSTART and RLENGTH which describes the substring of someVar which matched the pattern (0, -1 if no match).

It is less efficient to put a pattern in a variable than in //, because the // form is compiled in once at the start, but the variable is processed every time it is used.

However, I usually use a variable for complex patterns, because I can then give it a name that documents what it is for. Obviously, if the patterns is constructed from other stuff, then a variable is the only option you have.


  1. A-Za-z0-9_ ↩︎

Hi Manish,
I am not sure if you mean an shell variable or an awk variable. Let us say you want to insert shell script variable $1 into the regex in your awk script. All you do is keep $1 outside the ‘single quotes’ so the shell can expand it, and optionally write it as “$1” in case there are embedded spaces:

awk ‘/foo’“$1”‘bar/{print}’ input.file

If you want to use an awk variable in your trigger, grahamatittoolbox’s advice is for you, i.e.

awk ‘BEGIN{myawkvariable=“foo”}
index($0,myawkvariable){print}’ input.file

Cheers,
Wolfgang

Sorry I meant to say you can use a variable in you script like this:

Instead of:
/variable/ {print}
use:
index ($0, variable) {print}
This will match the content of ‘variable’ to the line in the same way you want /variable/ to
check out the awk man page for ‘index’
I tested it and it works for me

#!/usr/bin/awk -f
index ($0, “FOO”) {
print
}

Will that do?

#!/usr/bin/awk -f
index ($0, “FOO”) {
print
}

Will that do?

Thanks Brian & Mark

Hi Manish,

The first example I sent was the awk script itself. $dropzone
(the variable) was passed to it from the calling shell script
and concatenated with the data that the awk script was
processing, so indeed you can paste a variable value to your awk
script output. Here’s how the awk script is called from the
shell script (the shell script calls four different awk scripts
that process differently formatted flat files, but calls it the
same way each time). The $1, $2, $3 and $4 variables are
different flat files that are processed by the different awk
scripts.

awk -F , -f $dropzone/scripts/bodi_level3_file.awk
$dropzone/scripts/$1 >> $dropzone/scripts/$1$2$3$4.cpfiles

Cheers,
Mark

You can as long as you are talking about shell variables and letting the
shell expand them instead of awk. E.g.

$ VAR=test ; echo “abctest123” | awk “/$VAR/{print}”
abctest123
$ VAR=test ; echo “abctes123” | awk “/$VAR/{print}”
$

Brian

On 8/2/07, Manish via shellscript-l
wrote:

Thanks Mark,

But My question is can we use the parameter/variable as a part of
regulation expression ?

Thanks Mark,

But My question is can we use the parameter/variable as a part of regulation expression ?

Hi Manish,

Here’s an example. The awk script is called by a shell script that sets and exports the variable ($dropzone). This works in Korn shell.

Cheers,
Mark

bodi_source_processor.ak - process list files and move them from dropzone to target

input LEVEL3 file - top level directory, second level directory, file name

begin { fs=“,” }
{print “cp $dropzone/” $2 “/” $3 " /" $1 “/” $2 “/” $3}
end