awk tutorial – 7 awk printing examples
This is the first article in the new awk tutorial series. We will publish several articles on awk in the coming time, which will explain all the features of awk with practical examples.
In this article, let's review the basic awk working methods and 7 practical awk printing examples.
awk introduction and printing operations
awk is a programming language that can easily manipulate structured data and generate formatted reports. Awk stands for the names of its authors, " Aho , Weinberger , and Kernighan ."
Awk is mainly used for pattern scanning and processing. It searches one or more files to see if they contain lines that match the specified pattern, and then performs related operations.
Some key features of awk are:
- awk treats text files as records and fields.
- Like common programming languages, Awk has variables, conditionals, and loops.
- awk has arithmetic and string operators.
- awk can generate formatted reports
awk reads data from a file or from its standard input and outputs to its standard output. awk cannot process non-text files.
语法
awk '/search pattern1/ {Actions} /search pattern2/ {Actions}' file
In the above awk syntax:
- The search pattern is a regular expression.
- Actions - statements to be executed.
- There are many modes and operations that can be used in awk.
- file——input file.
- The single quotes around the program are to avoid any special characters in it being interpreted by the shell.
How awk works
awk reads the input file one line at a time.
- For each line, it matches the given patterns in the given order and if there is a match performs the corresponding action.
- If no pattern matches, no action is performed.
- In the above syntax, either the search pattern or the operation is optional, but not both.
- If no search pattern is given, Awk performs the given action on each line of input.
- If no action is given, all lines matching the given pattern are printed, which is the default action.
- Empty brackets without any action do nothing. It does not perform the default print action.
- Each statement in Actions should be separated by a semicolon.
Let us create employee.txt file with the following contents , which will be used in the examples mentioned below.
employee.txt
100 Thomas Manager Sales $5,000 200 Jason Developer Technology $5,500 300 Sanjay Sysadmin Technology $7,000 400 Nisha Manager Marketing $9,500 500 Randy DBA Technology $6,000
awk example 1. awk's default behavior
By default, Awk prints each line in a file.
$ awk '{print;}' employee.txt
In the above examples no pattern is given. Hence, these operations are applied to all rows.
By default, the action print without any arguments prints the entire line. So it successfully prints all the lines of the file. Action must be enclosed in curly braces.
awk example 2. Print lines matching a pattern
$ awk '/Thomas/
> /Nisha/' employee.txt
In the above example, it prints all the lines that match "Thomas" or "Nisha". It has two patterns. awk accepts any number of patterns, but each set (pattern and its corresponding action) must be separated by a newline character.
awk example 3. Print only specific fields
awk has many built-in variables. For each record, i.e. line, it by default splits the record separated by whitespace characters and stores it in $n
variables. If the line has 4 words, it will be stored in $1 , $2 , $3, and $4 . $0 represents the entire line. NF is a built-in variable that represents the total number of fields in a record.
$ awk '{print $2,$5;}' employee.txt
In the above example, $2
and $5
represent Name and Salary respectively . We can also use $NF
to get the salary, where $NF
represents the last field. In print
the statement, ,
is a connector.
awk example 4. Initialization and final operations
There are two important modes in awk, designated by the keywords BEGIN and END .
语法
BEGIN { Actions} {ACTION} # Action for everyline in a file END { Actions } # 这里是Awk注释
The actions specified in the BEGIN section will be executed before starting to read lines from the input.
The END action will be executed after the input line has been read and processed.
$ awk 'BEGIN {print "Name\tDesignation\tDepartment\tSalary";}
{print $2,"\t",$3,"\t",$4,"\t",$NF;}
END{print "Report Generated\n--------------";
}' employee.txt
In the above example, it prints the title and the last file of the report.
Awk Example 5. Find employees whose employee id is greater than 200
$ awk '$1 >200' employee.txt
300 Sanjay Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy DBA Technology $6,000
In the example above, the first field $1
is the employee ID. So if $1
it is greater than 200, then just do the default print action and print the entire row.
awk Example 6. Printing a list of employees in the technical department
Now the department name is available as the fourth field, so we need to check $4
if it matches the string " Technology " and print the line if so.
$ awk '$4 ~/Technology/' employee.txt
operator is ~
used to compare with the regular expression. If it matches the default action i.e. print the entire line will be executed.
awk Example 7. Print the number of employees in the technical department
The following example checks if the department is Technology and if so, increments the variable in Action count
that was initialized to zero in the BEGIN section.
$ awk 'BEGIN { count=0;}
$4 ~ /Technology/ { count++; }
END { print "Number of employees in Technology Dept =",count;}' employee.txt
Then at the end of the process, we simply print count
the value of , which gives us the number of employees in the Technology department.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Linux Sed Tutorial: 6 Examples of Sed Branching Operations
Publish Date:2025/04/06 Views:165 Category:OPERATING SYSTEM
-
Like any other programming language, sed also provides special branching commands to control the flow of the program. In this article, let's review the following two types of Sed branches. Sed unconditional branch Sed conditional branching
How to install GrayLog on centos
Publish Date:2025/04/06 Views:122 Category:OPERATING SYSTEM
-
Installing Graylog on CentOS requires the following steps: 1. Add EPEL and MongoDB repositories: sudo yum install epel-release -y sudo rpm -Uvh https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-server-4.4.4-1.el7
export command in Linux
Publish Date:2025/04/05 Views:109 Category:OPERATING SYSTEM
-
The Linux terminal has many environment variables that contain necessary information about the system. Also, applications may require some environment variables in order to execute. export This article will use the command to define an envi
tr Command in Linux Bash
Publish Date:2025/04/05 Views:54 Category:OPERATING SYSTEM
-
In Linux, we can use Bash scripts to perform string manipulation such as concatenation, truncation, and finding words in a text. tr This article will explain how to translate or remove characters using command in Linux Bash . tr Using comma
How to Copy Files and Directories Using Linux Terminal
Publish Date:2025/04/05 Views:122 Category:OPERATING SYSTEM
-
cp We can use the and commands in Linux Terminal rsync to copy files and directories. cp The command is generally used to copy files, while rsync the command is generally used to copy directories. Use cp the command to copy files We use com
Obtaining an IPv4 Address on Unix and Linux
Publish Date:2025/04/05 Views:200 Category:OPERATING SYSTEM
-
IP stands for Internet Protocol, which specifies the principles of Internet communications. An Internet Protocol (IP) address is a unique identifier for each device on the Internet, allowing data to be transferred between connected devices.
Changing permissions on folders and directories in Linux/Unix
Publish Date:2025/04/05 Views:79 Category:OPERATING SYSTEM
-
The file permission system is one of the fundamental security elements of the Linux operating system. These features allow us to adjust file permissions and access modes. We provide permissions based on appropriate rights to avoid vulnerabi
How to Delete a User Account in Linux
Publish Date:2025/04/05 Views:149 Category:OPERATING SYSTEM
-
In Linux, we can have multiple user accounts. Sometimes, we may need to remove some users to deny their access to the system. In such cases, we have to delete the user account. We can use userdel command line utilities to delete user accoun
Switching Users in Linux
Publish Date:2025/04/05 Views:158 Category:OPERATING SYSTEM
-
Like any operating system, Linux can have multiple user accounts. Sometimes we may need to use a different user account to access files or run commands that require permissions as root. This article explains how to switch between different