How to be "awk"ward.

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,498
Reaction score
3,234
Credits
31,331

Using AWK in Linux: A Comprehensive Guide​

AWK is a powerful and versatile tool for text processing and data manipulation in Linux. It allows you to perform a wide range of operations on text files, making it an essential tool for system administrators, developers, and data analysts. In this article, we'll explore some of the most useful features of AWK, along with practical examples.

1. Printing Specific Columns​

One of the most common uses of AWK is to print specific columns from a text file. For example, to print the first and second columns from a space-delimited text file, you can use the following command:

Code:
 awk '{print $1, $2}' file.txt

If your file is comma-delimited, you can specify the field separator using the -F option:

Code:
 awk -F, '{print $1, $2}' file.csv

2. Pattern Matching​

AWK allows you to print lines that match a specific pattern. For example, to print lines containing the word "error":

Code:
 awk '/error/' file.txt

3. Field Manipulation​

You can perform operations on specific fields. For example, to add the values in the first and second columns:

Code:
 awk '{print $1 + $2}' file.txt

4. Conditional Statements​

AWK supports if-else statements, allowing you to perform actions based on conditions. For example, to print lines where the value in the second column is greater than 50:

Code:
 awk '$2 > 50 {print $0}' file.txt

5. Built-in Functions​

AWK has several built-in functions for string and numeric operations. Here are a couple of examples:

  • To convert the first column to uppercase:
Code:
 awk '{print toupper($1)}' file.txt

  • To convert all text in a file to lower case:
Code:
 awk '{print tolower($0)}' file.txt

  • To convert the first character of each column to uppercase:
Code:
 awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2))}1' file.txt

6. BEGIN and END Blocks​

You can use the BEGIN and END blocks to perform actions before processing any lines and after processing all lines. For example, to print a header and footer:

Code:
 awk 'BEGIN {print "Header"} {print $0} END {print "Footer"}' file.txt

7. Field Separators​

You can change the field separator to process files with different delimiters. For example, to process a CSV file:

Code:
 awk -F, '{print $1, $2}' file.csv

8. Summing Columns​

You can sum the values in a column. For example, to sum the values in the second column:

Code:
 awk '{sum += $2} END {print sum}' file.txt


These examples demonstrate the versatility and power of AWK for text processing in Linux. Whether you're extracting specific columns, performing calculations, or manipulating text, AWK provides a robust set of tools to help you accomplish your tasks efficiently.
 


Members online


Top