views:

442

answers:

5

Or just a matter of choice and call awk and sed equivalent towards usage. They both do the common search replace seemingly identically regarding i/o.

+1  A: 

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.

Jamie Lewis
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
+1  A: 

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.

Rob Wells
GNU sed actually supports in place file editing.
anon
Yep. But, as you say, the -s option is GNU specific. Vanilla sed implementations are stream based. That's it.
Rob Wells
+4  A: 

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.

Tyler McHenry
I 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.
anon
+4  A: 

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.

anon
I 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.
e-satis
+2  A: 

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.

e-satis