7

Last few days and before that i ahve been asking questions about sed and awk. This happens everytime that when i ask awk question people say to use sed and when i ask sed question people say to use awk.

can anyone give me some examples on where i can use each one.

To me , awk was used to presentation and reporting no substitution and sed was used where something needs to replaced.

But still many people gave me examples where substition is done via awk with print commnad.

Now i am really confused what are the uses cases of that.

As an example. suppose

I have source code file in C++ and i want to add some code before particular pattern.

what should i use sed or awk. Pattern can span multiple lines as well

user175386049
  • 451
  • 1
  • 6
  • 12
  • This post may helps: http://stackoverflow.com/questions/366980/what-are-the-differences-between-perl-python-awk-and-sed – Lei Mou Jan 09 '13 at 06:28
  • I did read thats about other programming lnaguages as well in which sed , awk gets lost. I just want to now if we have only two options either sed or awk then what to use where – user175386049 Jan 09 '13 at 06:34

1 Answers1

10

The link in the comment is quite valuable. I would add some lines from my personal feeling of sed/awk usage.

I emphasis that, many text processing tasks could be done by both sed and awk. The text below are just my personal feeling.

  • if I want to do text processing, and write back to the original file without generating temp file, sed would win.

  • I like the sed's address, it could shorten the codes a lot. e.g. 3,6{s/a/b/}, 1~2{s/f/b/} or 3~5{s/f/b/}

  • if you want to pass the matched parts to an external commands/programs, awk is easier than sed (Gnu sed). If you want to have some fun and give it a try with sed, you could play a game: match a text, and pass parts (\1,\2 etc) to different sed. that is, sed nesting. after 3 times, I am lost... :)

  • if the text is somehow "column" based, in 99% case, awk won't let you down.

  • I would choose awk if I want to do different text processing on multi-files. e.g, {001-003}.txt different substitution requirements.

  • awk would be good at join-like processing among multi-files

  • if the logic needs some structure like if-else, for loop, while, number calculation etc, I would go with awk. though can sed in many cases do the job as well.

  • both sed and awk don't support Perl Regex. That's a pity.

  • So basically, I think awk is more flexible and straightforward than sed. If the task is relatively simple or there are well-known magic sed solutions (check http://sed.sourceforge.net/sed1line.txt there are many magic one-liners), I would go with sed. otherwise I would think awk first.

Both sed and awk are very powerful text processing tools, there are many text processing tools in linux/unix box, comm, cut, join, paste, sort, uniq... there are many option parameters of them. I am lazy and cannot remember all of them, just those common used ones. But If I know how to work with awk, sed and grep. 90% text processing under linux could be done. If awk, sed and grep cannot do the job, I would turn to a programming language. e.g. python.

well I am not a perl guy.

is there something that I forgot?

my 2cents, hope it helps.

Kent
  • 189,393
  • 32
  • 233
  • 301