×

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command stands for "Aho, Weinberger and Kernighan" and it works by reading text from a file or input stream, applying a set of commands or "awk script" to the text, and then writing the modified text to the output. The basic syntax for the 'awk' command is:

awk 'pattern { action }' [file/input-stream]

Where "pattern" is a regular expression that defines a condition for the action to be performed, "action" is the command or set of commands that you want to perform on the text that matches the pattern, and "file/input-stream" is the file or input stream that you want to read the text from.

One of the most basic and common uses of 'awk' command is to print specific fields of a text file. For example, if you have a file named 'file.txt' that contains a list of names and ages separated by a comma, and you want to print only the names, you would use the command:

awk '{print $1}' file.txt

The above command prints the first field of each line in the file 'file.txt'.

Another common usage of 'awk' command is to perform calculations on specific fields of a text file. For example, if you have a file named 'file.txt' that contains a list of prices and you want to calculate the total, you would use the command:

awk '{total+=$1} END {print total}' file.txt

The above command calculates the total of the first field of each line in the file 'file.txt' and prints the result at the end of the file.

The 'awk' command also provides options to specify the field separator and to perform conditional operations. For example, if you have a file named 'file.txt' that contains a list of names and ages separated by a colon, and you want to print only the names of people that are over 30, you would use the command:

awk -F: '{if($2>30) print $1}' file.txt

The above command specifies the field separator as a colon using the '-F' option, then it performs a conditional operation, checking if the second field is greater than 30, and if it is, it prints the first field.

In addition to these basic examples, the 'awk' command also supports more advanced operations such as loops, variables, and user-defined functions. You can use loops to iterate through the fields of a line, and use variables to store and manipulate data. You can also define your own functions to perform specific tasks.

It's also worth mentioning that, by default, 'awk' command reads the input from the standard input, which means you can use the command to process input from a pipe. For example, you can use the command to process the output of other commands, such as 'ls' or 'grep'.

It's important to note that, when using the 'awk' command, it's important to be careful with the script you're using. A small typo or mistake in the script can cause unexpected results and even data loss. It's also important to test the command and script on a small set of data before applying it to a large set of data.

It's also worth noting that there are other similar tools available for text processing in Linux, such as 'sed' and 'grep'. 'sed' is a stream editor that can perform basic text transformations on an input stream, such as substituting text. 'grep' is a command-line tool that can search for patterns in text files.

'awk' is often considered a more powerful tool than 'sed' and 'grep' because it has built-in support for variables, loops, and conditional statements, which makes it more suitable for more complex text processing tasks.

It's also important to note that 'awk' scripts can be written in different ways, using different conventions and styles. Some developers prefer using a more procedural style, where the script is written in a linear fashion, while others prefer using a more functional style, where the script is composed of small, reusable functions. The choice of style depends on the developer's preference and the specific requirements of the task.

In summary, the 'awk' command is a powerful and versatile text processing tool in Linux. It can be used to perform various operations on text files, such as searching, replacing, and manipulating text. It has built-in support for variables, loops, and conditional statements, which makes it more suitable for more complex text processing tasks. While it's an important command, it is important to be careful when using the command and to consider security implications when processing text files. With a good understanding of how the command works and its implications, you will be able to efficiently and effectively process text on your Linux systems.


Related Topics

Linux ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

10 minutes read.

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute read.

Linux pwd

The pwd command is used to print or display the name of the working or current directory. Some shells have their own version of the pwd with several features, in...

1 minute read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

Linux rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

1 minute read.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

4 minutes read.

unzip Command in Linux/Unix with Examples

The 'unzip' command in Linux is used to extract files from a ZIP archive file. The command allows you to decompress and extract the files and directories stored in a...

3 minutes read.

top Command in Linux/Unix with Examples

The 'top' command in Linux is a real-time system monitoring tool that displays information about the running processes and system resource usage. The command provides a dynamic, scrolling view of...

4 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

5 minutes read.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

apt-get Command in Linux with Examples

The apt-get command is a command-line tool that is used to handle packages in Linux. This command handles the processes like retrieving the packages and information from authentic sources for...

5 minutes read.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 minutes read.

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

wc Command in Linux/Unix with Examples

The wc command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a text file or standard input. The name...

4 minutes read.