Using shell variables
When it comes to automation and scripting, you'd often need to construct commands that can accept input from the user, incorporate data from a file or the output of a tool and so on.
In this chapter, you'll see how to pass information saved in shell variables to awk
commands. As mentioned before, this book assumes bash
as the shell being used.
As an example, see my repo ch: command help for a practical shell script where commands are constructed dynamically.
The example_files directory has all the files used in the examples.
-v option
The most common method is to use the -v
command line option.
# assume that the 's' variable is part of some bash script
# or perhaps a variable that stores the output of a shell command
$ s='cake'
$ awk -v word="$s" '$2==word' table.txt
blue cake mug shirt -7
ENVIRON
To access environment variables of the shell, you can call the special array variable ENVIRON
with the name of the environment variable as a string key.
# existing environment variable
# output shown here is for my machine, would differ for you
$ awk 'BEGIN{print ENVIRON["HOME"]}'
/home/learnbyexample
$ awk 'BEGIN{print ENVIRON["SHELL"]}'
/bin/bash
# defined along with the awk command
# note that the variable is placed as a prefix to the command
$ word='hello' awk 'BEGIN{print ENVIRON["word"]}'
hello
ENVIRON
is a good way to get around awk
's interpretation of escape sequences. This is especially helpful for fixed string matching (see the index section for examples).
$ s='hi\nbye'
# when passed via -v option
$ awk -v ip="$s" 'BEGIN{print ip}'
hi
bye
# when passed as an environment variable
$ ip="$s" awk 'BEGIN{print ENVIRON["ip"]}'
hi\nbye
Here's another example when a regexp is passed to an awk
command.
# when passed via -v option
$ r='\Bpar\B'
$ awk -v rgx="$r" '$0 ~ rgx' anchors.txt
awk: warning: escape sequence '\B' treated as plain 'B'
$ r='\\Bpar\\B'
$ awk -v rgx="$r" '$0 ~ rgx' anchors.txt
apparent effort
two spare computers
# when passed as an environment variable
$ r='\Bpar\B'
$ rgx="$r" awk '$0 ~ ENVIRON["rgx"]' anchors.txt
apparent effort
two spare computers
Summary
This short chapter revisited the -v
command line option and introduced the ENVIRON
special array. These are particularly useful when the awk
command is part of a shell script. Arrays will be discussed in more detail in the later chapters.
The next chapter will cover control structures.
Exercises
The exercises directory has all the files used in this section.
1) Use contents of the s
variable to display all matching lines from the input file sample.txt
. Assume that the s
variable doesn't have any regexp metacharacters and construct a solution such that only whole words are matched.
$ s='do'
##### add your solution here
Just do-it
2) Replace all occurrences of o
for the input file addr.txt
with the literal contents of the s
variable. Assume that the s
variable has regexp metacharacters.
$ s='\&/'
##### add your solution here
Hell\&/ W\&/rld
H\&/w are y\&/u
This game is g\&/\&/d
T\&/day is sunny
12345
Y\&/u are funny