How to Filter Command Output Using Awk and STDIN
In the previous parts of the Awk command series, we looked at reading input mostly from files, but what if you want to read input from STDIN?
In Part 7 of the Awk series, we shall look at a few examples where you can filter the output of other commands instead of reading input from a file.
List Files Owned by User in Directory
We shall start with the dir command, which works similarly to the ls command. In the first example below, we use the output of the dir -l
command as input for awk to print the owner’s username, group name, and the files he/she owns in the current directory:
dir -l | awk '{print $3, $4, $9;}'
List Files Owned by Root User
Take a look at another example where we employ awk expressions, here, we want to print files owned by the root user by using an expression to filter strings as in the awk command below:
dir -l | awk '$3=="root" {print $1,$3,$4, $9;} '
The command above includes the (==)
comparison operator to help us filter out files in the current directory that are owned by the root user. This is achieved using the expression $3==”root”
.
Use Awk Comparison Operator to Match String
Let us look at another example of where we use an awk comparison operator to match a certain string.
Here, we have used the cat command to view the contents of a file named tecmint_deals.txt and we want to view the deals of type Tech only, so we shall run the following commands:
cat tecmint_deals.txt cat tecmint_deals.txt | awk '$4 ~ /tech/{print}' cat tecmint_deals.txt | awk '$4 ~ /Tech/{print}'
In the example above, we have used the value ~ /pattern/
comparison operator, but there are two commands to try and bring out something very important.
When you run the command with pattern tech nothing is printed out because there is no deal of that type, but with Tech, you get deals of type Tech.
So always be careful when using this comparison operator, it is case-sensitive as we have seen above.
You can always use the output of another command instead as input for awk instead of reading input from a file, this is very simple as we have looked at in the examples above.
Filter Files Modified Today
Now to filter files modified today, you can use awk to extract and display these files:
ls -l | awk '$6 == "Aug" && $7 == "9" {print $9}'
This command filters files modified on August 9
Count Files of a Specific Type
To count the number of .txt
files in a directory, use:
ls -l | awk '$9 ~ /.txt$/ {count++} END {print count}'
Extract Specific Fields from Command Output
If you want to extract specific fields from a ps command output, such as the process ID and command name:
ps aux | awk '{print $1, $2, $11}'
Hope the examples were clear enough for you to understand, if you have any concerns, you can express them through the comment section below, and remember to check the next part of the series where we shall look at awk features such as variables, numeric expressions, and assignment operators.