The awk utility is a powerful tool for pattern scanning and processing in RHEL 8. It reads input data, processes it according to a set of user-defined rules, and can produce formatted reports.
Example 1: Printing Specific Fields
# This command prints the first and third fields of the /etc/passwd file, using a colon as the field separator.
awk -F':' '{print $1, $3}' /etc/passwd
Explanation: The -F':' option sets the field separator to a colon. The '{print $1, $3}' is the awk program that instructs it to print the first and third fields for each line of the /etc/passwd file.
Example 2: Filtering Lines Based on a Pattern
# This command searches for lines containing the word "bash" in /etc/passwd and prints the entire line.
awk '/bash/' /etc/passwd
Explanation: The command scans the /etc/passwd file. The pattern /bash/ matches any line containing the string "bash", and the default action is to print the entire matching line.
Example 3: Using Built-in Variables
# This command prints each line of the /etc/fstab file, preceded by its line number.
awk '{print NR, $0}' /etc/fstab
Explanation: NR is a built-in awk variable that holds the current record (line) number. $0 is another built-in variable that represents the entire current line. This command prints the line number followed by the line itself.
Example 4: Performing Arithmetic Operations
# This command calculates the sum of the third field for all lines in the 'data.txt' file.
awk '{sum += $3} END {print sum}' data.txt
Explanation: For each line in data.txt, the value of the third field is added to the sum variable. The END block is executed after all lines have been processed, printing the final value of sum.
Example 5: Conditional Statements
# This command prints the first field of lines in /etc/passwd where the third field (UID) is greater than 1000.
awk -F':' '$3 > 1000 {print $1}' /etc/passwd
Explanation: This command uses a conditional statement. If the value of the third field ($3) is greater than 1000, the associated action {print $1} is executed, printing the first field.
Example 6: Using BEGIN and END Blocks
# This command prints a header, then the first field of each line in /etc/group, and finally a footer.
awk 'BEGIN {print "Group Names:"} {print $1} END {print "End of List"}' /etc/group
Explanation: The BEGIN block is executed before any lines are processed, printing the header. The main block {print $1} is executed for each line. The END block is executed after all lines are processed, printing the footer.
Example 7: Field Manipulation and Output Formatting
# This command reverses the order of the first two fields of each line in 'names.txt'.
awk '{temp = $1; $1 = $2; $2 = temp; print $0}' names.txt
Explanation: A temporary variable temp is used to store the value of the first field. The first field is then replaced with the second, and the second field is replaced with the value stored in temp. Finally, the modified line is printed.
Example 8: Counting Occurrences
# This command counts the number of times each shell appears in the /etc/passwd file.
awk -F':' '{count[$NF]++} END {for (shell in count) print shell, count[shell]}' /etc/passwd
Explanation: An associative array count is used to store the frequency of each shell. $NF is a built-in variable that represents the last field. The END block iterates through the array and prints each shell and its corresponding count.