Next: Actions , Previous: Pattern Elements , Up: Patterns, Actions, and Variables [ Contents ][ Index ]
7.2 Using Shell Variables in Programs ¶
awk programs are often used as components in larger programs written in shell. For example, it is very common to use a shell variable to hold a pattern that the awk program searches for. There are two ways to get the value of the shell variable into the body of the awk program.
A common method is to use shell quoting to substitute the variable’s value into the program inside the script. For example, consider the following program:
The awk program consists of two pieces of quoted text that are concatenated together to form the program. The first part is double-quoted, which allows substitution of the pattern shell variable inside the quotes. The second part is single-quoted.
Variable substitution via quoting works, but can potentially be messy. It requires a good understanding of the shell’s quoting rules (see Shell Quoting Issues ), and it’s often difficult to correctly match up the different quotes when reading the program.
A better method is to use awk ’s variable assignment feature (see Assigning Variables on the Command Line ) to assign the shell variable’s value to an awk variable. Then use dynamic regexps to match the pattern (see Using Dynamic Regexps ). The following shows how to redo the previous example using this technique:
Now, the awk program is just one single-quoted string. The assignment ‘ -v pat="$pattern" ’ still requires double quotes, in case there is whitespace in the value of $pattern . The awk variable pat could be named pattern too, but that would be more confusing. Using a variable also provides more flexibility, as the variable can be used anywhere inside the program—for printing, as an array subscript, or for any other use—without requiring the quoting tricks at every point in the program.
How to Work with Awk Variables, Expressions, and Operators – Part 8
The Awk command series is getting exciting! I believe that, in the previous seven parts, we walked through some fundamentals of Awk that you need to master to enable you to perform basic text or string filtering in Linux.
Starting with this part, we shall dive into advanced areas of Awk to handle more complex text or string filtering operations. Therefore, we will cover Awk features such as variables , numeric expressions , and assignment operators .
These concepts are not significantly different from those you may have encountered in many programming languages before, such as Shell, C, Python, and many others. So, there is no need to worry much about this topic; we are simply revising the common ideas of using these features.
This will probably be one of the easiest Awk command sections to understand, so sit back and let’s get going
1. Awk Variables
In any programming language, a variable is a placeholder that stores a value . When you create a variable in a program file, as the file is executed, some space is created in memory that will store the value you specify for the variable.
You can define Awk variables in the same way you define shell variables as follows:
In the syntax above:
- variable_name : is the name you give a variable.
- value : the value stored in the variable.
Let’s look at some examples below:
Take a look at the simple examples above, in the first variable definition, the value tecmint.com is assigned to the variable computer_name .
Furthermore, the value 22 is assigned to the variable port_no , it is also possible to assign the value of one variable to another variable as in the last example where we assigned the value of computer_name to the variable server.
If you can recall, right from part 2 of this Awk series , where we covered field editing, we talked about how Awk divides input lines into fields and uses a standard field access operator, $ to read the different fields that have been parsed. We can also use variables to store the values of fields as follows.
In the examples above, the value of first_name is set to the second field and second_name is set to the third field.
As an illustration, consider a file named names.txt which contains a list of users indicating their first and last names plus gender.
Using the cat command , we can view the contents of the file as follows
Then, we can also use the variables first_name and second_name to store the first and second names of the first user on the list as by running the Awk command below:
Let us also take a look at another case, when you issue the command uname -a on your terminal, it prints out all your system information.
The second field contains your hostname , therefore we can store the hostname in a variable called hostname and print it using Awk as follows:
2. Numeric Expressions
In Awk , numeric expressions are built using the following numeric operators:
- * : multiplication operator
- + : addition operator
- / : division operator
- - : subtraction operator
- % : modulus operator
- ^ : exponentiation operator
The syntax for a numeric expression is:
In the form above, operand1 and operand2 can be numbers or variable names, and operator is any of the operators above.
Below are some examples to demonstrate how to build numeric expressions:
To understand the use of numeric expressions in Awk , we shall consider the following example below, with the file domains.txt which contains all domains owned by Tecmint .
To view the contents of the file, use the command below:
If we want to count the number of times the domain tecmint.com appears in the file, we can write a simple script to do that as follows:
After creating the script, save it and make it executable, when we run it with the file, domains.txt as our input, we get the following output:
From the output of the script, there are 6 lines in the file domains.txt which contain tecmint.com , to confirm that you can manually count them.
3. Assignment Operators
The last Awk feature we shall cover is assignment operators, there are several assignment operators in Awk and these include the following:
- *= : multiplication assignment operator
- += : addition assignment operator
- /= : division assignment operator
- -= : subtraction assignment operator
- %= : modulus assignment operator
- ^= : exponentiation assignment operator
The simplest syntax of an assignment operation in Awk is as follows:
You can use the assignment operators above to shorten assignment operations in Awk , consider the previous examples, we could perform the assignment in the following form:
Therefore, we can alter the Awk command in the shell script we just wrote above using += assignment operator as follows:
In this segment of the Awk command series , we covered some powerful Awk features, that is variables, building numeric expressions, and using assignment operators, plus a few illustrations of how we can actually use them.
These concepts are not any different from the ones in other programming languages but there may be some significant distinctions under Awk programming.
In part 9 , we shall look at more Awk features that are special patterns: BEGIN and END . Until then, stay connected to Tecmint .
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.
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 !
Previous article:
Next article:
Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.
Related Posts
How to Write Scripts Using Awk Programming Language – Part 13
How to Use Flow Control Statements in Awk – Part 12
How to Allow Awk to Use Shell Variables – Part 11
Learn How to Use Awk Built-in Variables – Part 10
How to Use Awk Special Patterns ‘BEGIN and END’ – Part 9
How to Read and Filter Awk Input from STDIN in Linux – Part 7
Got Something to Say? Join the Discussion... Cancel reply
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.
Save my name, email, and website in this browser for the next time I comment.
IMAGES
VIDEO