HMC Computer Science Department
This qref is written for a semi-knowledgable UNIX user who has just come up against a problem and has been advised to use awk to solve it. Perhaps one of the examples can be quickly modified for immediate use.
awk reads from a file or from its standard input, and outputs to its standard output. You will generally want to redirect that into a file, but that is not done in these examples just because it takes up space. awk does not get along with non-text files, like executables and FrameMaker files. If you need to edit those, use a binary editor like hexl-mode in emacs.
The most frustrating thing about trying to learn awk is getting your program past the shell's parser. The proper way is to use single quotes around the program, like so:
>awk '{print $0}' filename
The single quotes protect almost everything from the shell. In csh or tcsh, you still have to watch out for exclamation marks, but other than that, you're safe.
The second most frustrating thing about trying to learn awk is the lovely error messages:
awk '{print $0,}' filename
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1
gawk generally has better error messages. At least it tells you where in the line something went wrong:
gawk '{print $0,}' filename
gawk: cmd. line:1: {print $0,}
gawk: cmd. line:1: ^ parse error
gawk: cmd. line:1: {print $0,}
gawk: cmd. line:1: ^ parse error
So, if you're having problems getting awk syntax correct, switch to gawk for a while.