Introduction to AWK Command in Unix
AWK Command in Unix Shell Scripting is used for changing the data’s form and report generating. We can use numeric functions, logical operator, variables and string functions to manipulate the data without compiling. AWK command is generally used for processing the data and scanning the patterns in the file. It tries to scan one or more files to check if it contains the patterns that match the specified pattern required and finally does the required action on it. The AWK command is named from the initials of three people – Aho, Weinberger and Kernighan. They are from AT&T Bell Lab and has also contributed many other command-line prompts in Unix Shell Scripting.
AWK Command is used to perform the below actions:
- Define variables.
- Use numeric functions or Logical/Arithmetic operators, Variables or Strings.
- Apply loops or control flow actions.
- Generate reports.
Basic Syntax of AWK command:
awk [options] [program] [file]
What can we do from AWK Command?
Following are the list of Operations that we can do with AWK Command in Unix:
- AWK command initially tries to scan a file line by line.
- Divides each input file to fields to match the pattern.
- It then tries to compare the input file to the specified pattern.
- Finally, performs actions on the matched lines.
AWK command is useful for transforming the data files and then generating the reports from them. It is also used for performing arithmetic or logical operations on them to find out the matched patterns.
AWK Command is used to perform the below actions:
- Define variables.
- Use numeric functions or Logical/Arithmetic operators, Variables or Strings.
- Apply loops or control flow actions.
- Generate reports.
AWK Syntax:
awk [options] [selection action] [input_file]
Examples of How to Use AWK Command in Unix
Let us consider there is a file named ‘testing.txt’, where the contents in the file are below:
Code:
cat testing.txt
Output:
Example #1 – Option ‘print’.
To print all the contents in the file, awk helps to print all the line of the content in the file. In the below example, we have not used any pattern, so actions are applicable to the file lines. By default, action without any pattern argument will print the whole data/lines in the file. Hence, we can see that all the lines in the below file has been printed.
Syntax:
awk '{print}' file_name.txt
Code:
awk '{print}' testing.txt
Output:
Example #2 – Pattern match.
To print the specified pattern in the file, we can use the below syntax. It will list out the pattern matched in the input parameter given and will list out the data in the output.
Syntax:
awk '/pattern/ {print}' file_name.txt
Code:
awk '/student/ {print}' testing.txt
Output:
Example #3 – Splitting lines to fields.
We can also split the records in line with whitespace as its delimiter by default. For example, if we have 4 words in a line, it will be stored as $1, $2, $3 and $4, respectively, and $0 will print the complete line.
Syntax:
awk '{print $n, $m}' file_name.txt
Code:
awk '{print $2, $4}' testing.txt
Output:
Variables Built-in AWK Command in Unix
- NF: AWK Command with NF variable is used to count the number of fields in the input parameter passed.
- RS: AWK Command with RS is used to store the record separator in the current input parameter. By Default, a new line is considered to be a record separator if no specified variable is mentioned.
- NR: AWK command with its variable NR helps to count the number of input records. Generally, lines are considered as records.
- FS: While using the FS command, we need to specify the field delimiter. By default, white space is considered a delimiter if no specific delimiter is mentioned. It is used to divide the fields based on the input lines.
- ORS: AWK command with OFS variable is used to store the output record separator that is separated by AWK command to print the output lines. The new line character is treated as the default record separator.
- OFS: AWK command with OFS variable is used to store the output field separator when AWK prints the output. A blank space character is treated as a default field separator.
Examples of Variables in AWK Command
Given below are the examples mentioned:
Example #1 – Variable NF.
When we want to print the last line of the record in the file, we can use the awk command with the variable NF.
Syntax:
awk '{print $NF}' file_name.txt
Code:
awk '{print $NF}' testing.txt
Output:
Example #2 – Variable NR.
AWK command with its variable NR helps to count the number of input records. Generally, lines are considered as records.
Syntax:
awk '{print $NR <file separator> $n}' file-name.txt
Code:
awk '{print NR "-" $2}' testing.txt
Output:
Example #3 – Variable OFS.
AWK command with OFS variable is used to store the output field separator when AWK prints the output. A blank space character is treated as a default field separator.
Syntax:
awk 'OFS=”field_separator” {print $n, $m, $o}
Code:
date | awk 'OFS="/" {print $2, $3, $6}'
date | awk 'OFS="-" {print $2, $3, $6}'
Output:
Example #4 – Variable END.
To count the number of lines in the input file, we can use the END variable with the awk command as shown below.
Syntax:
awk 'END {print NR}' file_name.txt
Code:
awk 'END {print NR}' testing.txt
Output:
Example #5 – Variable BEGIN.
This BEGIN variable is used to set actions before any records have been executed. We can also print any data that we want to print before the records are processed, as below.
Syntax:
awk 'BEGIN { print “print your data”} ' file-name.txt
Code:
awk 'BEGIN {print "Starting of the line"}; {print $2}; ' testing.txt
Output:
Conclusion
AWK Command in Unix Shell Scripting is used for advanced level of text processing. It is generally used for pattern match and as a reporting tool. With the use of awk, we can define a set of actions that are to be executed on the input file. The input data that is passed as a parameter is divided into fields and records.
Recommended Articles
This is a guide to AWK Command in Unix. Here we discuss the introduction; what can we do from AWK command? Examples and variables built-in AWK command in Unix. You may also have a look at the following articles to learn more –
16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses