Mastering awk: The Ultimate Guide to Text Processing in Linux
This comprehensive tutorial explains the awk command in Linux, covering its origin, syntax, predefined variables, execution flow, functions, expressions, control structures, and practical examples, enabling readers to efficiently process and manipulate text files from the command line.
1. Introduction to awk
awk is a powerful text‑processing language used for analyzing and transforming data files. Its name comes from the initials of its creators—Alfred Aho, Peter Weinberger, and Brian Kernighan. In the Unix toolbox, awk joins grep and sed as the three "swordsmen" for text handling.
1.1 Basic Role
awk reads input line by line, splits each line into fields, and applies user‑defined actions.
2. awk Usage
2.1 Syntax
awk [options] 'pattern {action}' file2.2 Common Options
-F: specify the input field separator (default is whitespace).
2.3 Example
awk -F':' '{print $1}' /etc/passwd3. Predefined Variables
$0: the entire current line. $n: the nth field. NF: number of fields in the current line. NR: record number (line number). FS: input field separator. OFS: output field separator.
3.1 Example
awk -F':' '{print $NF, $1}' /etc/passwd4. Execution Flow
awk processes scripts in the order: BEGIN block → pattern/action pairs → END block. At least one of these blocks must be present.
5. Functions
print: prints fields with OFS between them. printf: formatted output using C‑style specifiers ( %s, %d, width, alignment, etc.).
5.1 Example
awk -F':' 'BEGIN{OFS=" >>> "}{printf "%+15s|%-15s
", $NF, $1}' /etc/passwd6. Pattern Matching and Positioning
Regular expressions can be used to select lines, e.g., /root/ {print $0} prints lines containing "root".
7. Comparison Expressions
>, <, >=,
<= ~: matches regex; !~: does not match.
7.1 Example
awk -F':' '$4 > $3 {print $0}' /etc/passwd8. Conditional Expressions
Use ==, !=, etc., to test values. Example: NR == 3 {print $0} prints the third line.
9. Logical Expressions
&&: logical AND ||: logical OR !: logical NOT
9.1 Example
awk -F':' '$3+$4 > 2000 && $3*$4 > 2000 {print $0}' /etc/passwd10. Arithmetic Expressions
Supported operators: +, -, *, /, %. Example: NR % 2 == 0 {print $0} prints even‑numbered lines.
11. Flow Control
if (condition) { … } else { … } for (i=10; i>0; i--) { print $0 } while (condition) { … }11.1 Example
awk -F':' '{if($3 > $4) print "greater"; else print "less or equal"}' /etc/passwd12. Practical Case
Print a separator line every five lines:
awk -F':' '{if(NR%5==0) print "----------"; print $0}' /etc/passwdFor further details, refer to the original source.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
