How to Allow Awk to Use Shell Variables – Part 11

When we write shell scripts, we normally include other smaller programs or commands such as Awk operations in our scripts. In the case of Awk, we have to find ways to pass some values from the shell to Awk operations.

This can be done by using shell variables within Awk commands, and in this part of the series, we shall learn how to allow Awk to use shell variables that may contain values we want to pass to Awk commands.

There are two possible ways you can enable Awk to use shell variables:

1. Using Shell Quoting

Let us take a look at an example to illustrate how you can actually use shell quoting to substitute the value of a shell variable in an Awk command.

In this example, we want to search for a username in the file /etc/passwd, filter, and print the user’s account information.

Therefore, we can write a test.sh script with the following content:

#!/bin/bash

# Read user input
read -p "Please enter username: " username

# Search for username in /etc/passwd file and print details on the screen
cat /etc/passwd | awk "/$username/ { print $0 }"

Thereafter, save the file and exit.

Interpretation of the Awk command in the test.sh script above:

cat /etc/passwd | awk "/$username/ "' { print $0 }'

"/$username/ " – shell quoting is used to substitute the value of the shell variable username in the Awk command. The value of the username is the pattern to be searched in the file /etc/passwd.

Note that the double quote is outside the Awk script, ‘{ print $0 }’.

Then make the script executable and run it as follows:

chmod  +x  test.sh
./text.sh 

After running the script, you will be prompted to enter a username, type a valid username, and hit Enter. You will view the user’s account details from the /etc/passwd file as below:

Shell Script to Find Username in Password File
Shell Script to Find Username in Password File

2. Using Awk’s Variable Assignment

This method is simpler and better compared to the first method. Considering the example above, we can run a simple command to accomplish the job. Under this method, we use the -v option to assign a shell variable to an Awk variable.

Firstly, create a shell variable, username and assign it the name that we want to search in the /etc/passswd file:

username="aaronkilik"

Then type the command below and hit Enter:

cat /etc/passwd | awk -v name="$username" ' $0 ~ name {print $0}'
Find Username in Password File Using Awk
Find Username in Password File Using Awk

Explanation of the above command:

  • -v – Awk option to declare a variable
  • username – is the shell variable
  • name – is the Awk variable

Let us take a careful look at $0 ~ name inside the Awk script, ' $0 ~ name {print $0}'. Remember, when we covered Awk comparison operators in Part 4 of this series, one of the comparison operators was value ~ pattern, which means: true if the value matches the pattern.

The output($0) of cat command piped to Awk matches the pattern (aaronkilik) which is the name we are searching for in /etc/passwd, as a result, the comparison operation is true. The line containing the user’s account information is then printed on the screen.

Conclusion

We have covered an important section of Awk features, that can help us use shell variables within Awk commands. Many times, you will write small Awk programs or commands within shell scripts and therefore, you need to have a clear understanding of how to use shell variables within Awk commands.

In the next part of the Awk series, we shall dive into yet another critical section of Awk features, which is flow control statements. So stay tuned and let’s keep learning and sharing.

For those seeking a comprehensive resource, we’ve compiled all the Awk series articles into a book, that includes 13 chapters and spans 41 pages, covering both basic and advanced Awk usage with practical examples.

Product Name Price Buy
eBook: Introducing the Awk Getting Started Guide for Beginners $8.99 [Buy Now]

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

2 Comments

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.