tags:

views:

797

answers:

3
  • What is the difference between awk and sed ?
  • What kind of application are best use cases for sed and awk tools ?
A: 

found this on the web.... it's a long reading... Sed and awk article

To use these command you really need to know what you're trying to do....

Dani
-1: anybody can google...
jldupont
@jldupont - are you referring to Rachel's original question or to Dani's reply? If it can apply to either than it can apply to both.
Dinah
@jldupont I don't understand what your point is, yes, given enough time, anyone can google something out, but if someone has done the work and already has a link to an informative answer and is providing it here, what is wrong with that?
yx
@jldupont Not everyone can google - if they could, there won't be these many questions in SO to which you're tempted to say lmgtfy. (I am just replying to his comment here - not suggesting that this is such a question - in fact, I upvoted the question and Dennis's answer).
Amarghosh
+2  A: 

1) What is the difference between awk and sed ?

Both are tools that transform text. BUT awk can do more things besides just manipulating text. Its a programming language by itself with most of the things you learn in programming, like arrays, loops, if/else flow control etc You can "program" in sed as well, but you won't want to maintain the code written in it.

2) What kind of application are best use cases for sed and awk tools ?

Conclusion: Use sed for very simple text parsing. Anything beyond that, awk is better. In fact, you can ditch sed altogether and just use awk. Since their functions overlap and awk can do more, just use awk. You will reduce your learning curve as well.

ghostdog74
+16  A: 

sed is a stream editor. It works with streams of characters on a per-line basis. It has a primitive programming language that includes goto-style loops and simple conditionals (in addition to pattern matching and address matching). There are essentially only two "variables": pattern space and hold space. Readability of scripts can be difficult. Mathematical operations are extraordinarily awkward at best.

awk is oriented toward delimited fields on a per-line basis. It has much more robust programming constructs including if/else, while, do/while and for (C-style and array iteration). There is complete support for variables and single-dimension associative arrays plus (IMO) kludgey multi-dimension arrays. Mathematical operations resemble those in C. It has printf and functions. The "K" in "AWK" stands for "Kernighan" as in "Kernighan and Ritchie" of the book "C Programming Language" fame (not to forget Aho and Weinberger). One could conceivably write a detector of academic plagiarism using awk.

Both programs use regular expressions for selecting and processing text.

I would tend to use sed where there are patterns in the text. For example, you could replace all the negative numbers in some text that are in the form "minus-sign followed by a sequence of digits" (e.g. "-231.45") with the "accountant's brackets" form (e.g. "(231.45)") using this (which has room for improvement):

sed 's/-\([0-9.]\+\)/(\1)/g' inputfile

I would use awk when the text looks more like rows and columns or, as awk refers to them "records" and "fields". If I was going to do a similar operation as above, but only on the third field in a simple comma delimited file I might do something like:

awk -F, 'BEGIN {OFS=","} {gsub("-([0-9.]+)","(&)",$3);print}' inputfile

Of course those are just very simple examples that don't illustrate the full range of capabilities that each has to offer.

Dennis Williamson
Thank you Dennis for the prompt explanation.
Rachel
To see some examples of pushing the boundaries of `sed`: http://sed.sourceforge.net/#scripts
Dennis Williamson
Thank you Dennis for providing the link to sed examples
Rachel