Skip to content
Advertisement

Pass argument to awk inside do loop

I have a large number of tab-separated text files containing a score I’m interested in in the second column:

test_score_1.txt

JavaScript

test_score_2.txt

JavaScript

I want to check if I have Titles with a score smaller than a number I define.

The following code defines my score in the script and works:

check_score_1

JavaScript

If I try to pass an argument to awk like so check_scores_2.sh "-7.5" as shown in check_score_2.sh, that returns all entries from both files.

check_scores_2.sh

JavaScript

Finally, check_scores_3.sh reveals that I’m actually not passing any arguments from my command line.

check_scores_3.sh

JavaScript

$ ./check_score_3.sh "-7.5" gives the following output:

JavaScript

What am I doing wrong?

Advertisement

Answer

In your shell script, the first argument to the shellscript is available as $1. You can assign that value to an awk variable as follows:

JavaScript

Discussion

  • Your print0/while read loop is very good. The -exec option offered by find, however, makes it possible to run the same command without any explicit looping.

  • The command {if ($2 < -7.5) print $0} can optionally be simplified to just the condition $2 < -7.5. This is because the default action for a condition is print $0.

  • Note that the references $1 and $2 are entirely unrelated to each other. Because $1 is in double-quotes, the shell substitutes in for it before the awk command starts to run. The shell interprets $1 to mean the first argument to the script. Because $2 appears in single quotes, the shell leaves it alone and it is interpreted by awk.  Awk interprets it to mean the second field of its current record.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement