The least you need to memorize about sed and awk

If you search for the usage of the sed/awk command on the Internet, you will get tons of tutorials teaching you how to use them. I have learnt sed/awk for dozen of years but still cannot write a valid sed/awk command without looking up their manuals. Why? Because their syntax and usage are too complex. You read it just to forget it. The problem is due to the reason that you’re not clear about the positioning of the sed/awk command in bash programming. Most usages of sed/awk are useless so do not try to memorize them. In bash programming, sed is used to get specific lines from a file or stand input. Awk is used to get specific fields of a line. With the help of both sed and awk, you can handle most tasks in bash programming.

One of the few things you need to memorize about sed is its syntax:

sed -options ‘script’ file

You must write option after – as other bash commands do. You must write script between ”. To get the nth line of a file, you must use the following command:

sed -n ’3p’ file

the -n option tells sed not to print lines to stdout as done by default. The script ’3p’ tells sed to print(p) the 3rd line.

Awk is more complicated than sed. The most important thing you should memorize about awk is also its syntax, not the dazzling usages.

awk -options ‘script’ file

You can see its syntax is the same as sed. But the script is more complex than sed, which includes pattern and action like ‘pattern{action}’. When awk scans a line that matches the pattern, it does the action. If we have used sed to get wanted lines, we can only use action for further processing, i.e., getting specific fields of the lines.  The only action worth memorizing is the print action.

awk ‘{print $1}’ file

$1 means the first field. Awk separates fields with space.


Posted in tips of hosting