6.5. When should I ignore sed and use awk or Perl instead?
If you can write the same script in awk or Perl and do it in less
time, then use Perl or awk. There's no reason to spend an hour
writing and debugging a sed script if you can do it in Perl in 10
minutes (assuming that you know Perl already) and if the processing
time or memory use is not a factor. Don't hunt pheasants with a .22
if you have a shotgun at your side . . . unless you simply enjoy
the challenge!
Specifically, use awk or perl if you need to:
- count fields or words on a line. (awk)
- count lines in a block or objects in a file.
- check lengths of strings or do math operations.
- handle very long lines or need very large buffers. (or gsed)
- handle binary data (control characters). (perl: binmode)
- loop through an array or list.
- test for file existence, filesize, or fileage.
- treat each paragraph as a line. (well, not always)