# Usage of a variable in awk BEGIN

**URL:** <https://community.unix.com/t/usage-of-a-variable-in-awk-begin/371100>\
**Category:** Shell Programming and Scripting\
**Created:** [February 26, 2018, 7:19am UTC](https://community.unix.com/t/usage-of-a-variable-in-awk-begin/371100 "2018-02-26T07:19:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nms](https://community.unix.com/letter_avatar/nms/32/6_449c533f586939b6e5f246632a46421f.png) [@nms](https://community.unix.com/u/nms)\
**Post date:** [February 26, 2018, 7:19am UTC](https://community.unix.com/t/usage-of-a-variable-in-awk-begin/371100/1 "2018-02-26T07:19:30Z")

</div>

Hi,

```nohighlight
diffcount=`awk 'BEGIN { while ( getline < "/scripts/matt/text.server1.reference" ) { arr[$0]++ } } { if (!( $0 in arr ) ) { print } }' $TMPDIR/$(basename $0 .sh) | wc -l`
        if [[$diffcount -eq 0]]; then
        OK="OK - No change in the interfaces status"
        elif [[$diffcount -gt 0]]; then
        DIFF=`awk 'BEGIN { while ( getline < "/scripts/matt/text.server1.reference" ) { arr[$0]++ } } { if (!( $0 in arr ) ) { print } }' $TMPDIR/$(basename $0 .sh).$1 | awk -F "|" '{print$2,"("$3")"}' ORS=' ' | awk '{for(i=1;i<NF;i++)if(i%2==0){$i=$i","} }1'

```

i have a part of a script where i can output the difference between two text files. If there is a difference it will output it in the same line, comma separated. All works well, apart that i cannot understand how a variable can be entered to represent the file path "/scripts/matt/ and the name of the file as $1 in script, where $1 = server1" as when i do that it fails. Note that after the getline function, the other file (in which text.server1.reference is being compared with) "$TMPDIR/$(basename $0 .sh)" works fine when pushed to a variable. So this should be something like:

```nohighlight
diffcount=`awk 'BEGIN { while ( getline < "/$MATTDIR/text.$1.reference" ) { arr[$0]++ } } { if (!( $0 in arr ) ) { print } }' $TMPDIR/$(basename $0 .sh) | wc -l`

```

Can you insert variables in the getline part?

Rgds,

Matt

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 26, 2018, 7:55am UTC](https://community.unix.com/t/usage-of-a-variable-in-awk-begin/371100/2 "2018-02-26T07:55:56Z")

</div>

Pass parameters into `awk` with the `-v` option:

```nohighlight
awk -vMDIR="$MATTDIR" ' ... "/scripts/" MDIR "..."

```

And, yes, you can `getline var < file` (c.f. `man awk` )  
And, how about saving one execution of your first `awk` script like

```nohighlight
diffcnt=$(awk '...' file | tee /tmp/WRK | wc -l)

```

and then work from the WRK file?

---

<div class="post-metadata">

**Author:** ![nms](https://community.unix.com/letter_avatar/nms/32/6_449c533f586939b6e5f246632a46421f.png) [@nms](https://community.unix.com/u/nms)\
**Post date:** [February 26, 2018, 12:33pm UTC](https://community.unix.com/t/usage-of-a-variable-in-awk-begin/371100/3 "2018-02-26T12:33:31Z")

</div>

Hi,

Thanks for that hint. i have tried to push the following variables into the -v, so command looks like this:

```nohighlight
HOMEDIR=/matt/scripts

awk -v MDIR="$HOMEDIR/text.$1.reference" 'BEGIN { while ( getline < "MDIR" ) { arr[$0]++ } } { if (!( $0 in arr ) ) { print } }' $TMPDIR/$(basename $0 .sh).$1 | awk -F "|" '{print$2,"("$3")"}' ORS=' ' | awk '{for(i=1;i<NF;i++)if(i%2==0){$i=$i","} }1

```

Note the $1 is the first argument given to the script (the hostname for example)

it takes a while, meaning that it fails. Running it with a verbose mode, i could see that variable assignment MDIR is taken into account as it displays the whole path including the $1 variable, however it seems i'm doing something wrong in the getline \< "MDIR". I tried also removing the "\<"

Rgds,

Matthew

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [February 26, 2018, 12:41pm UTC](https://community.unix.com/t/usage-of-a-variable-in-awk-begin/371100/4 "2018-02-26T12:41:48Z")

</div>

You should not quote the variable `MDIR` within awk code, because then it will be regarded as the string `"MDIR"` .

Also it is best to use: `while((getline ...)>0)` , seeing as `getline` returns 1 for a successful input, 0 for end of file, and -1 for an error...

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 26, 2018, 1:28pm UTC](https://community.unix.com/t/usage-of-a-variable-in-awk-begin/371100/5 "2018-02-26T13:28:22Z")

</div>

Why don't you consider combining those three `awk` s into one?
