Daily Hack #day21 - AWK

Cloud TunedCloud Tuned
2 min read

The awk command is a powerful text processing tool that's perfect for manipulating data files in Unix environments. Here are some useful tricks and hacks with awk:

File called data.txt contents:

banana, apple, avocado
orange, grapes, tomato
spinach, carrots, banana
orange, garlic, pear

Field Extraction:

This prints the first, second and third field of each line in the file.

awk '{print $1}' data.txt

output:

 banana,
 orange,
 spinach,
 orange
awk '{print $2}' data.txt

output:

 apple,
 grapes,
 carrots,
 garlic
awk '{print $3}' data.txt

output:

 avocado
 tomato
 banana
 pear

Field Separator:

awk -F',' '{print $1}' data.txt

output:

 banana
 orange
 spinach
 orange
awk -F',' '{print $2}' data.txt

output:

 apple
 grapes
 carrots
 garlic

This sets the field separator to en empty space (',') and prints the first field of each line. Due to the nature of our simple data.txt file, it has the same outcome as the first example bit without actually printing the comma.

Replacing Text:

awk '{gsub("banana", "berry", $0); print $0}' data.txt

output:

 berry, apple, avocado
 orange, grapes, tomato
 spinach, carrots, berry
 orange, garlic, pear

This replaces all occurrences of the old value banana with the new value berry in each line and prints the modified lines.

Counting Lines:

awk 'END {print NR}' data.txt

output:

 4

Unique Values:

Find out how many unique values are there in the first column.

awk -F, 'NR>0{print $1}' data.txt | sort | uniq | wc -l

output:

 3

Find out how many unique values are there in the second column.

awk -F, 'NR>0{print $2}' data.txt | sort | uniq | wc -l

output:

 4

These are just 4 simple examples of what the awk command can do. It's a versatile tool with a lot of potential for text processing tasks.

0
Subscribe to my newsletter

Read articles from Cloud Tuned directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Cloud Tuned
Cloud Tuned