Quote:
Originally Posted by
Ardzii
rather than understand the limitations between the 3 commands (grep | sed | awk).
As vgersh99 already noted "grep" comes from "g/re/p", which is a (schematised) sed-command. Let us see if i can help to improve your understanding:
grep is basically a line filter: you feed it a stream of input (or files) and it displays all the lines matching a certain pattern. Options can i.e. reverse this matching (so effectively all lines NOT matching the pattern can be displayed), you can count the found lines ("-c"), etc. but basically that is it: filtering out lines containing some text pattern from text.
grep is a good tool to find out about the existence of certain text and all that is related to this:
grep -c "pattern" /some/file - count the number of lines containing "pattern"
grep -v "pattern" /some/file - display all lines NOT containing "pattern"
if grep -q "pattern" /some/file ; then -
grep -q makes grep not display any lines, matched or otherwise. But since grep will exit with "0" if it has found anything and with "1" if it hasn't this will execute the following code if "pattern" was anywhere in the file.
Now
sed:
sed is for "stream editor" and this is exactly what it is: a highly programmable text editor. You feed it some input text (from a file or a stream of data) but instead of just searching it (like grep) you can also manipulate and change it. If you want to filter out certain lines AND at the same time change the text found by some rules (like cutting out a certain part of the line, but also more complicated things)
sed is the tool to turn to.
I am halfways through mustering the energy to write a
sed-introduction, so watch out for "the most incomplete introduction to sed" (i only write "most incomplete" articles). To write it in a few sentences is just too complicated, i am sorry. What makes
sed similar to
grep is that it uses the same "regular expressions" to describe text patterns. So once you learn how to use grep and its powerful pattern-matching engine you can use this knowledge in
sed too.
Lastly
awk:
awk stands for "Aho, Weinberger, Kernighan", its three primary authors. It is a regular programming language with some reminiscences of C and it sports a very similar regular expression engine as
sed (the sed variant is called UNIX BRE - basic regular expressions, the awk variant UNIX ERE - extended regular expressions). Again, its scope overlaps with sed and for many problems here you will find a sed-solution along with an awk-solution.
awk has a built-in structure for the evaluation of data files: each awk-program consists of three parts: one that is executed before any input is read, one that is executed for every line of input and one that is executed after all input is processed. If you want to set up the program and draw some header, then process the input line by line, finally do some end-processing like drawing footers, sums and the like this is ideal.
OK, this is a quick and very incomplete overview of what the tools do. You will find that all three of them have their purpose. If you picture the UNIX toolbox as an orchestra waiting for a gifted conductor to make them sound great (you), especially
sed and
awk are the exceptionally gifted soloists. You will find that they have their quirks (as all the great artists do) but work with them can be immensely rewarding and five minutes of truly guiding them to their limit will compensate for the weeks of hard work in rehearsal.
I hope this helps.
bakunin