,

Top 20 Commands with explanation of “awk” command

Posted by

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

The awk command in Linux is a powerful text-processing tool used for pattern scanning, extracting, formatting, and manipulating text files, logs, CSVs, and structured data. It reads files line by line, splits them into fields, and performs actions on them.

Letโ€™s go deep into 20 powerful awk commands, explained in a humanized and detailed way.


1. Print the Entire File

awk '{print}' file.txt

โœ” How it works:

  • {print} tells awk to print every line.
  • This is equivalent to cat file.txt, but awk allows modifications.

2. Print Only Specific Columns

awk '{print $1, $3}' file.txt

โœ” How it works:

  • $1 โ†’ Represents the first column.
  • $3 โ†’ Represents the third column.
  • Example: If file.txt contains:
John 25 Developer
Sarah 30 Designer

Output:

John Developer
Sarah Designer
  • This is useful for CSV files and log analysis.

3. Print Lines Matching a Pattern

awk '/error/ {print}' log.txt

โœ” How it works:

  • Searches for “error” in log.txt and prints the matching lines.
  • Example:
INFO: Process started
ERROR: File not found
WARNING: Disk space low

Output:

ERROR: File not found

โœ” Alternative:
To match case-insensitively:

awk 'tolower($0) ~ /error/' log.txt

4. Print Lines Where a Column Matches a Value

awk '$2 == "30" {print $1}' file.txt

โœ” How it works:

  • If the second column is “30”, print the first column.

๐Ÿ“Œ Example Input (file.txt):

John 25 Developer
Sarah 30 Designer
Mike 30 Manager

Output:

Sarah
Mike

5. Print Only Lines Where a Number is Greater Than a Value

awk '$2 > 25' file.txt

โœ” How it works:

  • Prints lines where the second column is greater than 25.

6. Print Line Number Along with Each Line

awk '{print NR, $0}' file.txt

โœ” How it works:

  • NR โ†’ Stands for “Number of Record” (line number).

๐Ÿ“Œ Example:

1 John 25 Developer
2 Sarah 30 Designer
3 Mike 40 Engineer

7. Print Total Number of Lines

awk 'END {print NR}' file.txt

โœ” How it works:

  • END executes after processing all lines.
  • NR gives the last line number (total lines).

8. Calculate the Sum of a Column

awk '{sum += $2} END {print "Total:", sum}' file.txt

โœ” How it works:

  • Adds all values in column 2 and prints the total.

๐Ÿ“Œ Example Input:

John 25
Sarah 30
Mike 40

Output:

Total: 95

9. Calculate Average of a Column

awk '{sum += $2; count++} END {print "Average:", sum/count}' file.txt

โœ” How it works:

  • Sums up column 2 and divides by count.

10. Print Only Even-Numbered Lines

awk 'NR % 2 == 0' file.txt

โœ” How it works:

  • NR % 2 == 0 means print only even lines.

11. Print Only Odd-Numbered Lines

awk 'NR % 2 == 1' file.txt

โœ” How it works:

  • NR % 2 == 1 means print only odd lines.

12. Print Only the Last Column

awk '{print $NF}' file.txt

โœ” How it works:

  • $NF โ†’ Represents the last field in each line.

๐Ÿ“Œ Example Input:

John 25 Developer
Sarah 30 Designer
Mike 40 Engineer

Output:

Developer
Designer
Engineer

13. Print Only the First and Last Column

awk '{print $1, $NF}' file.txt

โœ” How it works:

  • Prints first and last column in each line.

14. Change Column Separator (FS)

awk -F',' '{print $1, $3}' file.csv

โœ” How it works:

  • -F',' sets , as the field separator for CSV files.

15. Replace a Word in a Column

awk '{gsub("old", "new", $2); print}' file.txt

โœ” How it works:

  • Replaces “old” with “new” only in column 2.

16. Extract Unique Values from a Column

awk '!seen[$2]++' file.txt

โœ” How it works:

  • Uses an associative array to filter duplicates from column 2.

17. Count Occurrences of Words

awk '{count[$1]++} END {for (word in count) print word, count[word]}' file.txt

โœ” How it works:

  • Counts occurrences of each unique word in column 1.

๐Ÿ“Œ Example Input:

Apple
Banana
Apple
Mango
Banana

Output:

Apple 2
Banana 2
Mango 1

18. Find the Longest Line

awk '{if (length > max) {max = length; line = $0}} END {print line}' file.txt

โœ” How it works:

  • Finds the longest line in a file.

19. Extract Lines Between Two Patterns

awk '/START/,/END/' file.txt

โœ” How it works:

  • Prints everything between “START” and “END”.

20. Execute Shell Commands in awk

awk 'BEGIN { system("date") }'

โœ” How it works:

  • Runs date before executing awk.

Final Thoughts

  • awk is an advanced text-processing tool, making it essential for system administrators, data analysts, and programmers.
  • These 20 commands cover real-world applications, including data extraction, calculations, filtering, and reporting.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x