I used sed for a batch ptovess where I could not do it with awk. Vould awk have done it? Or is it more a matter of choice and call awk and sed equivalent for the usage. They both do the common search replace similar with i/o. Is there a good example what can't be done with one that the other can?
5 Answers
One main difference is that an awk program can maintain state and can operate using multiple passes over the same data. A sed invocation is necessarily stateless single-pass because sed (Stream EDitor) is inherently stream-oriented. The advantage, though, is that this makes sed simpler and more appropriate for using in pipe chains.

- 74,820
- 18
- 121
- 166
-
2I don't see that sed is "more appropriate for using in pipe chains" - just because awk CAN perform multiple passes, nothing forces you to actually do so. For many pipe-related tasks (selecting specific fields, for example) awk is much simpler to use than sed. – Aug 30 '09 at 10:55
-
@anon It strikes me as odd how you are turning this thread in some "awk vs sed" stuff. Not helpful at all. – ata Oct 19 '11 at 11:14
In the original (and still the best) book, The AWK Programming Language, the following are implemented (among many other things):
- a simple assembler
- recursive descent compiler
- a text indexing program
Try doing that with sed.
-
2I think that this is answer to a complete different question. The question is "What’s what awk can’t that sed can?", not "what can awk do that sed can't". Just saying that "awk is better, stronger, faster" does not answer the question at all. – Bite code Aug 31 '09 at 13:43
G'day,
Awk is more powerful. sed tends to be more limited in what it can do.
Sed is good for line-based changes to data. It has some simple looping constructs, the usual ed/ex/vi regexp stuff and substitution things, compound statements, decisions etc. Most people use it for modifying piped data.
Awk is good for filtering or rearranging data. It mostly gets used for reporting.
I'd suggest having a look at Dale Dougherty's excellent book "sed & awk" (sanitised Amazon link). BTW It's got one of the best explanations of regexps in there as well.
Many people would say use Perl anyway! (-:
Edit: Forgot to say that the awk language is quite C like which is no surprise given that the 'k' in awk stands for Brian Kernighan. Yes. That Brian Kernighan!
Also, sed only works on data streams whereas awk works on both data streams and files.
HTH
cheers,Sed only works on data streams whereas awk works on both data streams and files.

- 36,220
- 13
- 81
- 146
-
Yep. But, as you say, the -s option is GNU specific. Vanilla sed implementations are stream based. That's it. – Rob Wells Aug 30 '09 at 11:37
The only thing I can think of is that sed can do little changes in less char count the awk. So for quick tweaks on a live shell, it's faster to type.

- 578,959
- 113
- 301
- 329
SED is a stream editor, and therefore does not have variables and a few other constructs like AWK has. AWK is a fully fledged language.

- 4,928
- 1
- 24
- 20
-
Is AWK , unlike sed, good for finding and replacing given any unicode or ascii code? so, treating all control characters including new line, EOF, e.t.c. as binary ? – barlop Aug 27 '10 at 06:35