Knowing the essentials of the “awk” command is very important when it comes to processing data efficiently, and this post covers the key features of the “awk” command. Let’s check the syntax first:
Some of the commonly used options are given in the table below:
Option | Description |
-F | To specify a file separator |
-f | Specify the file that contains the “awk” script |
-v | To assign variable |
Let’s take a look at some examples about the usage of the “awk” command, and for demonstration, I have made a text file by the name of testFile.txt:
1. How to print a column of a file with the awk command?
The “awk” command can be used to get a specific column of the text file. To print the content of file use:
Now, to print the second column of the file, use:
To print more than one fields, use the command:
If you don’t use the comma “,” then the output will be without spaces:
2. How to use regular expression with awk command:
To match the strings or any expression, we use slashes “//,” for instance, if you want to print the names of people who are studying “History,” then use:
The output is clearly showing that only “Sam” and “Tommy” are studying the “History” course.
3. How to use the relational expression with the “awk” command:
To match the content of a specific field, relational expression can be used. To match any string or expression against a field, indicate the field and use the comparison “~” operator with the pattern as presented in the following command:
The above output displaying every field in column 2 against every field that contains “is” in column 3.
And to get the opposite output of the above command, use the “! ~” operator:
For comparison, we can also use operators like greater than “>” and less than “<” and equal “=” as well:
The output has printed the names of people who got marks of more than 70.
4. How to use range pattern with awk command:
A range can also be used for search; simply use the comma “,” to separate the range as presented in the below-mentioned command:
The output shows the subjects of the range from “Joel” to “Marlene” from column 2. We can use the double equal sign “==” to define a range; see the example below:
The output displays the names of the people from column 2 for the range of marks “70 to 80” from column 4.
5. How to combine pattern using logical operator:
The use of logical operators such as OR “||,” AND “&&” allow you to combine patterns for search. Use the following command
The above command prints people’s names against the fourth field more significant than 80 and the sixth field greater than 0.4. And only two records are fulfilling the condition.
6. The awk command special expressions:
There two special expressions, “BEGIN” and “END”:
BEGIN: To perform an action before data is processed
END: To perform an action after the data is processed
7. The useful built-in variable of awk command:
The awk command has various variables that help in data processing:
Variable | Description |
NF | It gives the number of fields in the data |
NR | It gives the number of the current record |
FILENAME | Displays the name of the file that is currently being processed |
FS and OFS | Field separator and Output Field separator |
RS and ORS | Separates the record and Output Record Separator |
For example:
We use “END,” but if you use “BEGIN,” the output would give 0 fields and 0 records.
8. How to change the record separator:
The default separator in the record is usually space; if there is a comma “,” or dot “.” as your field separator, then use the “FS” option along with the separator.
Let’s have another file where data fields are separated by a comma colons “:”:
$awk ‘BEGIN {FS= “:”} {print $2}’ testFile2.txt
Since the file’s separator is a colon, but the “awk” command even beneficial for the files like this, simply use the “FS” option.
The “-F” can also be used:
The default record separator is “newline,” and to set the record separator to “:”, use:
9. Awk Actions:
Awk actions are tiny programs that are surrounded by “{}” brackets and have more than one statement separated by semi-colons“;”.
The most used statement with the “awk” command is the “print” statement. For example, to print a text with each record, use text string in quotes:
Let’s perform a simple sum operation using awk:
10. Creating an awk program:
Let’s begin with the “awk” programming, the programming given below is simply doing multiplication:
i=2
while(j<4)
{
print “The multiplication of 2 with” j “ is ” i*j;
j++
}
}
Save the program by the name of “myCode.awk” and to run it, open terminal and type:
Conclusion:
The “awk” command is a handy command to process, scan data of text files, such as separating any particular field of a file; we use the “awk” command. It makes it easier to search anything in any form or pattern from the text files. In this guide, we understand the basics of the “awk” command and its usage. The “awk” command validates data, generates reports, and even parses files. Using simple commands “awk” also enables users to write tiny programs to process data more efficiently.