VehiclesFashionRecipesBlogsHuntTravelsSportFunHandmadeITEducation
Mini-Games
x

x
zakruti.com » IT - Software » IT, programs, coding
Why Use CAT Or GREP When You Can AWK? DistroTube

Why Use CAT Or GREP When You Can AWK? DistroTube

FBTwitterReddit

video description

Rating: 4.0; Vote: 1
Why Use CAT Or GREP When You Can AWK? DistroTube Enough of the bloat! You don't need to use cat, grep, head, sed, etc. All you need is AWK! - https://www.gnu.org/software/gawk/manual/gawk.html Gregory: For beginners 'bc', 'cat', 'curl', 'head', 'grep', 'sed', 'tail', 'wc', and 'wget' are easier to learn for text retrieval and processing; the subprocess operator $() comes in handy too. In contrast, 'awk' is a text-processing utility with a built-in programming language. Those 5 utilities plus 'gnuplot' are sufficient for data analysis and much of data science. If programming language constructs such as variables, loops, and user-defined functions are needed, a standard -nix shell like 'bash' provides these features. By the way, I use all of these utilities and sometimes only the full capabilities of ''awk' on a daily basis to perform all stages of the data science workflow, including machine learning. Whether I choose to go awk-less depends on the project. Okay, I am sadistic. By the way, I use Python and R too but when introducing data analysis or the stages of the data science workflow up to exploratory data analysis. The 'bc', 'cat', 'curl', 'head', 'grep', 'sed', 'tail', 'wc', and 'wget' utilities allow for an interactive, step-wise refinement approach known as REPL without the cognitive overhead of programming language constructs.
Date: 2022-03-30

Comments and reviews: 8


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

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

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

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

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

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

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

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