awk Command in Linux
Overview
The awk command is a versatile tool that is used for text processing and manipulation in Linux. It is primarily used for data extraction, analysis and formatting of text data. The command is available in most Linux distributions and is an essential tool for any Linux system administrator.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Linux awk Command Syntax
The syntax for the awk command is as follows:
Where:
- options: Various options available for awk command.
- pattern: Specifies a pattern to search for in the input data.
- action: Specifies the action to take on the input data that matches the specified pattern.
- filename: Specifies the name of the input file.
awk Command Options:
- -F: Sets the field separator.
- -v: Creates a variable.
- -f: Specifies an awk script file to use.
- -i: Modifies the input file directly.
- -o: Writes the output to a file.
- -W: Specifies the output format.
Example Usages
-
Prints the second column of a file separated by space.:
Output:
Explanation: This command prints the second column of the file specified by the filename, separated by space. The $2 specifies the second column.
-
Prints lines containing the word 'error'.:
Output:
Explanation: This command prints all lines containing the word 'error' from the file specified by the filename. The /error/ specifies the pattern to search for, and the { print } specifies the action to take when a match is found.
Turn Learning into Career Growth
Tips
-
The field separator can be any regular expression.
-
The pattern can be a regular expression.
-
The action can be any valid awk expression.
-
By default, the entire line is printed when a pattern is matched.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Advanced Use Cases of awk Command in Linux
-
Prints the sum of the values in the third column.:
Output:
Explanation: This command prints the sum of the values in the third column of the file specified by the filename. The { sum += $3 } adds the value of the third column to the sum variable for each line, and the END { print sum } prints the final sum.
-
Prints the average of the values in the third column.:
Output:
Explanation: This command prints the average of the values in the third column of the file specified by the filename. The { sum += $3 } adds the value of the third column to the sum variable for each line, and the END { print sum/NR } prints the sum divided by the number of records.
-
Prints the lines between two patterns.:
Output:
Explanation: This command prints all lines between the start_pattern and end_pattern in the file specified by the filename. The /start_pattern/, /end_pattern/ specifies the range of lines to print.
Conclusion
-
AWK is a powerful text processing tool that is used for data extraction, analysis and formatting.
-
It supports regular expressions, variables, arrays, and flow control statements.
-
The basic syntax of the awk command consists of a pattern and an action, which specifies what to match and what to do when a match is found.
-
AWK is an essential tool for any Linux system administrator.