awk
is a very powerful programming language whereas grep
is just a filtration tool.[1]
Many tasks which can be accomplished using grep
(typically in conjunction with other commands) can potentially be done using a single awk
command.[1:1]
Basically, whatever
grep
can do, you can also do inawk
. Therefore, you should never see someone usinggrep
andawk
together.[2]
Using grep
and awk
grep "foo" file.txt | awk '{print $1}'
Using only awk
awk '/foo/ {print $1}' file.txt
How to use the awk
command to…
Search a directory for files or folders with a modified time of today’s date:
ls -lrt --time-style=+'%m-%d-%Y' | awk -v d=$(date +%m-%d-%Y) '$6==d'
How to use the grep
command to…
Search a directory for files with a modified time of today’s date:
ls -lrt --time-style=+'%d-%m-%Y' * | grep " $(date +'%d-%m-%Y') "
Tell grep
to display file modified time with hours, minutes, and seconds:
ls -lrt --time-style=+'%r %d-%m-%Y' * | grep " $(date +'%d-%m-%Y') "