1

When I pass the Command Line arguments to my script, We have to access that value by using $1, $2 etc., Similarly, when we are using awk, we can mention the columns of a file as $1,$2 etc.,

 #!/bin/bash
 cat $1 | awk '$1 > 20'
  • Let us assume I have passed a file name as a command line argument, and we can refer that argument by using $1.
  • Let us assume the file content is as follows.

     20 A         
     10 B   
     5  D  
     13 K
     50 C
    

How can I tell, when reading this code, whether it will access the command line $1 -- the input file name -- or the first column of the output from cat?

2 Answers2

2

There's an easy rule here: The shell does not perform any expansions on single-quoted strings.

This is true in other scenarios as well: echo '$1' prints the exact string $1.

Thus, in the following code (edited for correctness -- lack of double quotes around the shell variable caused a bug, and use of cat had needless inefficiency):

awk '$1 > 20' <"$1"

...we have $1 > 20 treated as literal by the shell and parsed as code only by awk, and "$1" -- being in double quotes, expanded by the shell.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    So if the first argument to the script is "myfile.txt", this runs the awk script `$1 > 20`with input from myfile.txt. Conversely, if you swap the double- and single-quotes to `awk "$1 > 20" <'$1'`, it would run the awk script `myfile.txt > 20` (which doesn't make any sense), and try to read input from a file literally named "$1" (which doesn't make much sense either). Basically, it's all determined by how things are quoted. – Gordon Davisson Nov 22 '17 at 23:02
  • @charles Duffy, Now If I want to take 20 as the Second command line Argument, Now I have to make my code as this [awk '$1 > "$2" ' < "$1"], But the code is displaying entire Data. Any Help !! – Naga Venkatesh Gavini Nov 23 '17 at 04:44
  • 1
    That's almost certainly incorrect quoting, but we can't guess what you are actually trying to say. Perhaps something like `awk -v limit="$2" '$1 > limit' <"$1"` (though really the redirection is superfluous here; like most file-oriented commands, Awk accepts a list of input files to read). – tripleee Nov 23 '17 at 06:39
  • 1
    @NagaVenkateshGavini, if that's your problem, there's a separate question for it: [How do I use shell variables in an awk script?](https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script) – Charles Duffy Nov 23 '17 at 15:38
0

Is there is any another way to represent the columns while using awk in the shell script?

You can represent them using variables For example

awk '{for(i=1;i<NF;i++){print $i}}' file

Here NF is the total number of fields(which may be considered as columns.) As i gets different values in loop the $i would expand to individual columns like $1, $2 and so.

sjsam
  • 21,411
  • 5
  • 55
  • 102