Usage of a variable in awk BEGIN


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Usage of a variable in awk BEGIN
# 1  
Old 02-26-2018
Usage of a variable in awk BEGIN

Hi,

Code:
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:

Code:
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
# 2  
Old 02-26-2018
Pass parameters into awk with the -v option:
Code:
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
Code:
diffcnt=$(awk '...' file | tee /tmp/WRK | wc -l)

and then work from the WRK file?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-26-2018
Hi,

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

Code:
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
# 4  
Old 02-26-2018
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...
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-26-2018
Why don't you consider combining those three awks into one?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: use variable defined in begin

Hi there, I'm working with file more than 400K lines, 60 columns. Column count is going to be multiple of 12: 60, 12, 72 or so. NF/12 gives me on how many iterations I've to do to check certain value. For example: 7, 14th if only 24 columns in file. 7th, 14th and 21st if 36 columns in... (6 Replies)
Discussion started by: genome
6 Replies

2. Shell Programming and Scripting

Awk: BEGIN: prints nothing

My code fails to do anything if I've BEGIN block in it: Run the awk script as: awk -f ~/bin/sum_dupli_gene.awk make_gene_probe.txt #!/usr/bin/awk -f BEGIN { print ARGV #--loads of stuff } END{ #more stuff } (14 Replies)
Discussion started by: genome
14 Replies

3. UNIX for Dummies Questions & Answers

awk search with begin

Hi, I have written below script to begin if the line has n #!/bin/ksh /usr/xpg4/bin/awk {/ n / 'BEGIN {X = "01"; X = "02"; X = "03"; X = "04"; X = "05"; X = "06"; X = "07"; X = "08"; X ="09"; X = "10"; X = "11"; X = "12"; };} NR > 1 {print $1 "\t" $5 "," X "," $6 " " $7}'} input.txt |... (9 Replies)
Discussion started by: stew
9 Replies

4. Shell Programming and Scripting

BEGIN and END format in awk

I'm new to awk, trying to understand the basics. I'm trying to reset the counter everytime the program gets a new file to check. I figured in the BEGIN part it would work, but it doesn't. #!/bin/awk -f BEGIN {counter=0} { sum=0 for ( i=1; i<=NF;... (1 Reply)
Discussion started by: guitarist684
1 Replies

5. Shell Programming and Scripting

Confused with the usage of one variable usage

Hi All I am not able to understand the usage of d# in the below variable declaration. FILE_LOC contains the directory path And also help me to know about what will be saved in the variable j. Thanks!!! j=${d#${FILE_LOC}/} (2 Replies)
Discussion started by: mohanm
2 Replies

6. Shell Programming and Scripting

awk BEGIN problem

awk 'BEGIN { print "line one\nline two\nline three" }' After ./awktest.sh Usage: awk -f progfile file ... Usage: awk 'program' file ... POSIX options: GNU long options: -f progfile --file=progfile -F fs --field-separator=fs -v var=val ... (7 Replies)
Discussion started by: cola
7 Replies

7. Shell Programming and Scripting

awk getting stuck after BEGIN

I am beginner in awk awk 'BEGIN{for(i=1;(getline<"opnoise")>0;i++) arr=$1}{print arr}' In the above script, opnoise is a file, I am reading it into an array and then printing the value corresponding to index 20. Well this is not my real objective, but I have posted this example to describe... (1 Reply)
Discussion started by: akshaykr2
1 Replies

8. Shell Programming and Scripting

Alias to awk BEGIN statement

I'd like to define an alias to awk's begin statement since I use awk with different delimiters all the time and it is tiresome to type awk '{OFS="\t";FS="\t"}{BLAH BLAH}' every time. The problem is that bash won't let me make an alias with an open quote, which is necessary for the BEGIN alias to... (3 Replies)
Discussion started by: baconbasher
3 Replies

9. Shell Programming and Scripting

AWK not processing BEGIN directive

Hello I have the following awk script: BEGIN { {FS = " " } {print "\t\tIllegal Loggon Attempts on MAIL\n"} {"date" | getline d} {printf "\t %s\n",d } {print "Loggon Name\t\t\t Number of Attempts\n"} ... (2 Replies)
Discussion started by: mojoman
2 Replies

10. Shell Programming and Scripting

Another question on awk - Begin statement

Hi, I've a question on awk. In English I want to: (a) open a file, (b) search through the file for records where length of field15 > 20 characters and (c) print out some fields in the record. I've written the following and it works OK. The trouble is this will ALWAYS write out the column... (5 Replies)
Discussion started by: eff_cee
5 Replies
Login or Register to Ask a Question