0
0
Linux CLIscripting~15 mins

awk patterns and actions in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
Learn awk Patterns and Actions
📖 Scenario: You have a small text file listing fruits and their quantities. You want to use awk to find and display fruits with quantities greater than a certain number.
🎯 Goal: Build an awk command that filters and prints fruits with quantities above a threshold.
📋 What You'll Learn
Create a text file named fruits.txt with exact content
Define a variable threshold with a numeric value
Write an awk command using a pattern to filter lines where quantity is greater than threshold
Print the fruit name and quantity for matching lines
💡 Why This Matters
🌍 Real World
Filtering and processing text data files is common in system administration and data analysis.
💼 Career
Knowing how to use awk patterns and actions helps automate log analysis, report generation, and data extraction tasks.
Progress0 / 4 steps
1
Create the data file fruits.txt
Create a file named fruits.txt with these exact lines:
apple 10
banana 5
cherry 12
date 7
elderberry 3
Linux CLI
Need a hint?

Use echo with -e and redirect output to fruits.txt.

2
Set the quantity threshold variable
Create a shell variable named threshold and set it to 6
Linux CLI
Need a hint?

Use threshold=6 to set the variable.

3
Write the awk command with pattern and action
Write an awk command that reads fruits.txt and prints the fruit name and quantity only if the quantity (second column) is greater than the shell variable threshold. Use the pattern $2 > threshold and print both fields.
Linux CLI
Need a hint?

Use awk -v threshold="$threshold" '$2 > threshold {print $1, $2}' fruits.txt.

4
Run the awk command and display output
Run the awk command from Step 3 and print the output showing fruits with quantity greater than threshold.
Linux CLI
Need a hint?

The output should list fruits with quantities above 6.