Tutorial #14 - awk: Basic Processing of data in files


1.  Log on to Unix if necessary.

2. List the lines in forsorting that are numbered 7 or higher.  

If your awk command doesn't work and you can't figure out why,
 try enclosing the program portion in single quotes (like the grep 
'pattern' argument), as in:
	awk 'NR >= 7 { print }' forsorting

3. Create a file named Mawk that will contain a line to print the 

third field three times in a row for every line containing the 

character M.

4. Add an END pattern to Mawk that will skip a line, then print a 

comment.

5. Use Mawk to locate lines in forsorting. (i.e. Mawk also outputs line numbers)

6. If you expect to use awk in your work, practice with it a bit 

more:  Pipe a long list of your current directory to awk and print 

only the directory names.

7. Pipe the date command to awk and print only the first two 

fields.  If your output had no space between these fields, redo it to

produce a single space between the two fields.  If your output had

a space between these fields, redo it to eliminate the space.


If it doesn't work:

A. With a single line command, you may have to use single quotes; 
be sure they enclose the pattern as well as the action.

B. The command to use the program file sould be 
awk -f Mawk forsorting . Remember to use the append symbol (>>) 
when you add the END pattern to Mawk.

C. Use the action: print $1 $2 for achieving concatenation of fields 
one and two; Use the action print $1, $2 to get a single space 
between fields one and two.

Questions? Robert Katz:rkatz@ned.highline.edu
Last Update April 7, 2005