
Why Use CAT Or GREP When You Can AWK? DistroTube
video description
Date: 2022-03-30
Related videos
Comments and reviews: 8
typecasto
Why not replace all 3 of these with python? Why bother using awk? Awk really only manipulates text, we really should be using something more versatile.
The reason we use those programs is because they're simpler. We use cat because we don't want to write an awk program to print a file. We use grep because we don't want to write a program to search a file. We use awk because we don't want to write an entire program just to manipulate text. We even use python because we don't want to write machine code.
Awk works worse for each of these tasks you've shown. It's worse than cat performance wise, it doesn't have colored output like grep does, the syntax is harder to read than sed, and it's lengthier to type in every instance.
I really like your content but this one totally misses the mark.
reply
Why not replace all 3 of these with python? Why bother using awk? Awk really only manipulates text, we really should be using something more versatile.
The reason we use those programs is because they're simpler. We use cat because we don't want to write an awk program to print a file. We use grep because we don't want to write a program to search a file. We use awk because we don't want to write an entire program just to manipulate text. We even use python because we don't want to write machine code.
Awk works worse for each of these tasks you've shown. It's worse than cat performance wise, it doesn't have colored output like grep does, the syntax is harder to read than sed, and it's lengthier to type in every instance.
I really like your content but this one totally misses the mark.
reply
Eduard
cat and grep and more and tail are the basis of UNIX systems. Do one thing and do it well. Pipe through the different things you are doing. For me it is easier to remember that cat echoes the file and grep gets the input and filters it in that to remember that grep has all those parameters one of witch is the filename. It sure has ANDs also, yet I use to pipe one grep to another if I am filtering on two conditions. In any case, the examples you give are longer to type and harder to remember that easy cat/grep equivalents.
Offtopic: After 30 years of using grep for different piped tasks i discovered -grep -r 'someCodeSnippet'- that looks into a folder and shows you all files and file lines that contains a given text. That is soo usefull, I cannot believe I never used it.
reply
cat and grep and more and tail are the basis of UNIX systems. Do one thing and do it well. Pipe through the different things you are doing. For me it is easier to remember that cat echoes the file and grep gets the input and filters it in that to remember that grep has all those parameters one of witch is the filename. It sure has ANDs also, yet I use to pipe one grep to another if I am filtering on two conditions. In any case, the examples you give are longer to type and harder to remember that easy cat/grep equivalents.
Offtopic: After 30 years of using grep for different piped tasks i discovered -grep -r 'someCodeSnippet'- that looks into a folder and shows you all files and file lines that contains a given text. That is soo usefull, I cannot believe I never used it.
reply
John
grep -P is not possible with awk; cat is such a common case it deserves its own program (imagine typing awk '-print-' file-.txt every time).
Likewise, how does one implement grep -n port -.cc in awk?
(I don't, for example, know how to match the start of a file, necessary to have a per file line number, if multiple files are specified on the command line -- grep -n gives you the filename and line number _in that file_, rather than the line number of all input files concatenated, and this is much more useful than what you can do with awk, before one adds the colour coded output you get from modern grep.)
reply
grep -P is not possible with awk; cat is such a common case it deserves its own program (imagine typing awk '-print-' file-.txt every time).
Likewise, how does one implement grep -n port -.cc in awk?
(I don't, for example, know how to match the start of a file, necessary to have a per file line number, if multiple files are specified on the command line -- grep -n gives you the filename and line number _in that file_, rather than the line number of all input files concatenated, and this is much more useful than what you can do with awk, before one adds the colour coded output you get from modern grep.)
reply
TheSulross
At some point these classic Unix tools just become verbs for what we want to do - when we just want to dump files as is, or combine several files into one, we achieve that via the verb of 'cat'. When we want to scrape things per line out of a file, we 'grep' it. If we want to scrape, filter/transform a file, we 'awk' it. Even if there were one uber tool that could do all these things, I'd have to create aliases for 'cat', 'grep', 'awk' so those verbs would still be there
reply
At some point these classic Unix tools just become verbs for what we want to do - when we just want to dump files as is, or combine several files into one, we achieve that via the verb of 'cat'. When we want to scrape things per line out of a file, we 'grep' it. If we want to scrape, filter/transform a file, we 'awk' it. Even if there were one uber tool that could do all these things, I'd have to create aliases for 'cat', 'grep', 'awk' so those verbs would still be there
reply
Thomas
Why use one tool for everything when every tool has a function it's best at? AWK is awesome, but sometimes it's just overkill with overhead. Not arguing against your video, I think a lot of people don't understand just how great awk can be or are intimidated by it so any exposure is good exposure. I myself have piped lots of commands together before I realized I could just use awk to get the same results myself.
reply
Why use one tool for everything when every tool has a function it's best at? AWK is awesome, but sometimes it's just overkill with overhead. Not arguing against your video, I think a lot of people don't understand just how great awk can be or are intimidated by it so any exposure is good exposure. I myself have piped lots of commands together before I realized I could just use awk to get the same results myself.
reply
waldmatias
Great video... but you answered yourself really. The simplest solution (or more readable) is usually the best one... so which do you prefer to use in a script?
This: head file.sh
Or this: awk '(NR>=0 && NR ... etc etc about 20 more characters... ' file.sh?
Which would be more readable and maintainable while also allowing fellow programmers looking at your code to 'get it' quickly?
reply
Great video... but you answered yourself really. The simplest solution (or more readable) is usually the best one... so which do you prefer to use in a script?
This: head file.sh
Or this: awk '(NR>=0 && NR ... etc etc about 20 more characters... ' file.sh?
Which would be more readable and maintainable while also allowing fellow programmers looking at your code to 'get it' quickly?
reply
Takis
This was an interesting video. However, if I want the head of a file, I will type: head t.txt not the long awk command you suggested. Have you counted how many keystrokes are involved in the awk command? The awk utility is only to be used for complex operations, which other simpler utilities cannot perform. It is not a replacement for all other commands.
reply
This was an interesting video. However, if I want the head of a file, I will type: head t.txt not the long awk command you suggested. Have you counted how many keystrokes are involved in the awk command? The awk utility is only to be used for complex operations, which other simpler utilities cannot perform. It is not a replacement for all other commands.
reply
Aurich
Tested on my slowest device
$ time awk '-print $0-' /usr/share/dict/american-english
awk '-print $0-' /usr/share/dict/american-english 0.15s user 0.26s system 97% cpu 0.419 total
$ time cat /usr/share/dict/american-english
cat /usr/share/dict/american-english 0.00s user 0.13s system 65% cpu 0.206 total
reply
Tested on my slowest device
$ time awk '-print $0-' /usr/share/dict/american-english
awk '-print $0-' /usr/share/dict/american-english 0.15s user 0.26s system 97% cpu 0.419 total
$ time cat /usr/share/dict/american-english
cat /usr/share/dict/american-english 0.00s user 0.13s system 65% cpu 0.206 total
reply
Add a review, comment
Other channel videos















