Recently I learned sed
for a while and it was awkward experience due to
- need of remembering single-letter commands (
p
,a
,d
, ...) - concept of single hold space for remembering the context – controlled by other similar commands (
h
,x
,g
, ...)
I said to myself: couldn't this be made more intuitive? I.e. with more verbose commands (e.g. print
instead of p
) and more than one buffer – standard variables according to my need (e.g. lastUserSeen
)?
And then someone told me:
No, never use
h
or any other sed constructs except s, g, and p (with -n) or you are literally using constructs that became obsolete in the mid-1970s when awk was invented. People use those other constructs today strictly for the mental exercise, not to seriously write software, since an equivalent awk solution will be clearer, more efficient, more portable, easier to maintain/enhance, and better in every other meaningful way.
awk? I immediately jumped on and fell in love with it. Many of my multi-line programs worked on first run, without any debugging. From what I since then studied about the awk, I think it is able to completely supersede the sed. Or is there some sed feature (which I missed) effect of which cannot be easily achieved by the awk? Is there any reason why an awk user would run sed?
Edit: this is not a question "which is better", but a comparison of feature sets. Is there a feature in sed effect of which cannot be achieved similarly efficiently in awk, but it would need much more awk code?
sed
has pros and cons likeawk
,perl
orpython
have. If you know to handle different tools, you can choose the best for a specific problem. The simple concept ofsed
and single char commands can be view as advantage for quick, compact scripts. You see it as disadvantage. So this is mainly opinion based.sed -i
is a good example, because it cannot be equivalently efficiently implemented in awk without in-place editing extension.awk
from Ed but his comment over there is waaaay off... Any seasonedsed
user will tell you that statement is BS.sed
is just as portable asawk
and just as easy to maintain if the maintainer is equally proficient with both of them. And never useh
,x
,d
etc ? Such comments are usually made by people who cannot fully understand howsed
operates on a stream of data. Try doing something likesed '$d'
orsed 'x;$!d'
withawk
, see if you can make it "more efficient" or golf it shorter. Each has its pros and cons so yes, this is primarily opinion based.awk
one liner example which is the equivalent of 11sed
commands ;) ... lol, come on man, can't you see it all depends on the input, the task at hand and the human operator ? Read, learn, practice, get better and you will know when to use one and when to use the other.