Does sed have a feature superior to awk?

miroxlav

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?

steve

For a quick and easy way to edit a file in place, sed -i feature is a whole lot easier than constructing something similar using awk. I accept that platforms using gawk at version 1.4.0 or later do have the "inplace" feature to cover this.

sed -i 's/foo/bar/' filename

Reluctantly learned sed myself due to all the existing scripts using it. Given a choice, when writing a new script, awk usually wins, the code is so much more readable + maintainable.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事