Tutorials  /  Linux Basics

From Pattern Recognition to Data Manipulation: The Versatility of AWK in Linux

Ccentron Redaktion · October 2024 ·4 min read ·Linux Basics, Tutorial

Discover the unlimited possibilities of AWK for text processing in Linux/Unix! From pattern recognition to data manipulation - learn how to efficiently complete your tasks with this powerful command. Dive into the world of AWK and optimize your text processing tasks today!

Functionalities of AWK:

  • Scanning files line by line
  • Splitting each input line into fields
  • Comparing input lines and fields to patterns
  • Executing specific actions on matching lines
VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Usefulness of the AWK command:

  • Modifying data files
  • Creating formatted reports

Programming concepts for the AWK command:

  • Formatting output lines
  • Conditions and loops
  • Arithmetic and string operations

AWK Syntax:

The syntax of the AWK command is:

Console
$ awk options 'selection_criteria {action}' input_file > output_file

To demonstrate AWK usage, we use the text file "file.txt", where the first column is "Item", the second column is "Model", the third column is "Country", and the fourth column is "Cost".

AWK Command Examples:

Printing specific columns:

Console
$ awk '{print $2 "\t" $3}' file.txt
Output:

Output:

Code
BMW    Germany
Volvo  Sweden
Subaru Japan
Ferrari Italy
SAAB   USA

Printing all lines in a file:

Console
$ awk '{print $0}' file.txt

Output:

Code
1 BMW Germany 25000
2 Volvo Sweden 15000
3 Subaru Japan 2500
4 Ferrari Italy 2000000
5 SAAB USA 3000

Printing lines matching a specific pattern:

Console
$ awk '/PATTERN/{print $0}' file.txt

Example:

Console
$ awk '/o/ {print $0}' file.txt
Output:
2 Volvo Sweden 15000
5 SAAB USA 3000 

Printing columns matching a specific pattern:

Console
$ awk '/PATTERN/{print $3 "\t" $4}' file.txt

Example:

Console
$ awk '/a/ {print $3 "\t" $4}' file.txt

Output:

Code
Japan   2500
USA     3000

Counting and printing matching patterns:

Console
$ awk '/PATTERN/{++cnt} END {print "Count = ", cnt}' file.txt

Example:

Console
$ awk '/a/{++cnt} END {print "Count = ", cnt}' file.txt

Output:

Code
Count = 2

Printing lines with more or less than a specified number of characters:

Console
$ awk 'length($0) > 20' file.txt

Output:

Code
1 BMW Germany 25000
2 Volvo Sweden 15000
3 Subaru Japan 2500
4 Ferrari Italy 2000000
5 SAAB USA 3000

Saving AWK output to another file:

Console
$ awk '/PATTERN/{print $3 "\t" $4}' file.txt > Output.txt

Example:

Console
$ awk '/a/ {print $3 "\t" $4}' file.txt > Output.txt

Conclusion:

AWK is another simple programming script that you can use to manipulate text in documents or perform specific functions. The commands shared here are only a few of many you can discover. With AWK, you have a powerful tool to automate and simplify your text processing tasks in Linux/Unix environments.

You might also be interested in:

Practical Tips for Converting Strings in C++

Overcoming Language Barriers: Integrating C Functionality into Python Applications

HttpClient: Making HTTP Requests in Java Applications Easy and Explained

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren