Both are worthwhile, but if I could only choose one I'd make it AWK. AWK programs tend to be easier for me to read.
AWK has features and constructs common to high level programming languages. If you already know any C-like language, AWK should not be difficult to learn. Sed uses a language similar to GNU Ed, meaning single-symbol commands and a heavy reliance on regular expressions.
AWK sees use for complex tasks, such a Wiki software. Sed mostly sees use for simple substitutions and deletions. People have done some very cool things in both languages though. I seem to recall that someone wrote an RPN calculator in Sed.
Here are some examples of specific programs in AWK and Sed. Fields are separated by single `\t' (tab) characters. Additions, elaborations and corrections welcome.
Output entire file
Code:
test$ awk '{print}' birthday-cake.txt
Janet June chocolate
Ken June chocolate
Jeff November vanilla
Dan January vanilla
test$ sed '#' birthday-cake.txt
Janet June chocolate
Ken June chocolate
Jeff November vanilla
Dan January vanilla
test$
Find lines containing specific text
Code:
test$ awk '/November/' birthday-cake.txt
Jeff November vanilla
test$ sed '/November/!d' birthday-cake.txt
Jeff November vanilla
test$
Replace specific text in specific field
Code:
test$ awk 'BEGIN {OFS = "\t"}; $3 == "vanilla" {$3 = "strawberry"}; {print}' birthday-cake.txt
Janet June chocolate
Ken June chocolate
Jeff November strawberry
Dan January strawberry
test$ sed 's/^\([^'$'\t''][^'$'\t'']*'$'\t''[^'$'\t''][^'$'\t'']*'$'\t''\)vanilla/\1strawberry/' birthday-cake.txt
Janet June chocolate
Ken June chocolate
Jeff November strawberry
Dan January strawberry
test$
Don't let that last one scare you. The
$'\t' bit is a Bash-ism for an explicit `\t' (tab) character. I had to make sure that the substitution occurred in the third field only, so it was necessary to count the tab characters between fields. My Sed-fu is weak; surely there is a better way.
I recommend reading the manuals for each program, and then decide for yourself which one seems best for the task at hand. Here is
the manual for GNU Sed.
Most GNU/Linux systems probably have Gawk or Mawk, though there are other implementations available. It is important to know which implementation you have because they are not all the same. The command
awk -Wversion should tell you which AWK you have. I recommend reading the
GNU Awk User's Guide in addition to the manual for your AWK, as it is the most complete reference to the laguage I'm aware of.
HTH