Prepared by Douglas Chen for CSC209F '98
Note that the awk language cannot be fully explained in just few examples. For those people who are interested in more aspects of awk, books on this topic can be found in any computer bookstore.
Here are some examples:
% awk '{ print $NF : $0}' CSC209F is an interesting course. [Ctrl-d] 5: CSC209F is an interesting course.The above example takes data from stdin (adding a file name at the end of the command line allows awk to read data from the file specified) and prints out number of fields (NF), a colon, and the entire line ($0).
% awk '/Ann/' name.list MarryAnne Ann DeAnneThis example is the same as "grep Ann name.list" which lists all occurrence of the pattern Ann in the file name.list.
% finger Login Name Host TTY Idle LoginTime ange Angela Glinos cheetah console 4d Fri 09:26 at209che Douglas I-Hsi Chen eddie pts/46 Tue 09:37 jdd John DiMarco mammoth pts/41 4 Tue 09:33 % finger | awk '{ print $1, $3}' ange cheetah at209che eddie jdd mammothThis example shows how to print the first field ($1) and the third field ($3) of each line piped from finger. The default input field separator (FS) is a white space. You can change its value to a comma (,) for example by using the -F option (i.e. finger | awk -F, '{print $1, $3}' or finger | awk 'BEGIN{FS=","} {print $1, $3}')
See also: sed, grep
chmod
Chmod modifies the file permission associated with a file. The default file
permission on CDF when a file is created is 600 (or -rw-------). Note that
6 in 600 is the sum of r (4) and w (2). The execution permission
x has value of 1. Therefore the permission 741 is equivalent to
-rwxr----x.
Here's how you can change the permission to something else.
(original permission, -rw-------) % chmod 644 myfile (now change to -rw-r--r--) % chmod a=r myfile (now change to -r--r--r--) % chmod ug+w myfile (now change to -r--rw-rw-)See also: umask
Zcat is equivalent to 'uncompress -c'
% compress myfile (myfile becomes myfile.Z) % uncompress myfile.Z (myfile.Z becomes myfile) % uncompress -c myfile.Z (dump content of myfile.Z to standard output, myfile.Z does NOT become myfile) % zcat myfile.Z (same as above) % zcat myfile.Z > myfile (redirect stdout to a file)See also: gzip/gunzip
% date '+DATE: %m/%d/%y%nTIME: %H:%M:%S' (result) DATE: 09/28/98 TIME: 14:45:05
% diff file1 file2 (basic use of diff) % diff -i file1 file2 (tell diff to ignore case)Try options: -c, -e, -f, -h, -n
See also: cmp
grep
Grep is perhaps the most useful tool you will encounter in software
development. It is a pattern matching tool allowing users to specify a pattern
using regular expression. Commonly used commands are as follows.
Note: story.txt is the text file to be searched % grep green story.txt (show all lines that contain 'green' in them) % grep -i green story.txt (ignore case for the word 'green') % grep -v green story.txt (show all lines that does not have 'green') % grep ^green story.txt (show all lines that begins with 'green')Try options: -A, -B, -e, -f See also: sed
Here are some examples:
% gzip myfile (produces gzip'ed myfile.gz) % gunzip myfile.gz (restore myfile.gz to myfile) % gzip -d myfile.gz (same as above) % gzip -cd myfile.gz (dump content of myfile.gz to stdout, user can then pipe stdout to more/less) % gzip -cd myfile.tar.gz | tar xf - (gunzip and untar the file in one command)See also: tar/untar
% head myfile (output first 10 lines of myfile) % head -c 1k (output first 1-kilobyte of myfile, subs k with b (byte) or m (megabyte)) % tail -5 myfile (output last 5 lines of myfile) % head -15 myfile | tail -10 (output line 5 to line 15 of myfile)Try: tail -f myfile
Note that the -P option is available to all three programs and can be used to select a printer other than the default one or specified in the PRINTER environment variable. If you don't like to use the -P option every time, modify the PRINTER variable!
Here are some examples:
% lpr story.txt finale.ps (prints out story.txt and finale.ps) % lpr -Preuse * (prints all files and redirect to printer 'reuse') % lpq (show status of default printer) % lpq -Preduce (show status of printer 'reduce') % lprm 1023 (remove printing job 1023 from printing queue)Try lpr options: -T, -U, -F, -R, -i
% ls -a (list all files including .*) % ls -d (list directory entries instead of contents) % ls -i (list inode of each file) % ls -l (list files in long format) % ls .* (list files begin with .)Try options: -ld, -u, -g, -r, -t
% man man (show man's own manual page) % man -k jump (keyword search on 'jump')Try options: -f, -M
See also: lpr, lpq, lprm, quota
ps
This command is used to see the status of current processes.
Try 'ps', 'ps -a', 'ps -x' and see the man page for more
information.
pwd
Pwd prints the name of current (present) working directory.
% pwd /home/u2/csc209h/a209abcd
See also: pquota
sed
Sed is the stream editor. It is one of the most powerful and useful tools
like grep for shell scripting, etc. It evolves around
regular expressions, therefore, it is important to gain some basic understand
about the topic before using sed.
Here are some examples:
% sed 's/John/Johnson/g' report.txt (change all occurrence of John to Johnson in report.txt) % sed 'y/abc/ABC/' report.txt (replace all occurrence of a to A, b to B and c to C) % sed '1,10 d' report.txt (delete the first 10 lines)See also: sed's man page
sort -n Sort in numeric order sort -nr Sort in reverse numeric order sort +See also: grepSort starting at +1-st field sort -r Reverse normal order sort -u Discard duplicates (display unique lines)
(at one directory above directory 209, do the following) % tar cf 209.tar 209 (create a tar file 209.tar which contains everything in the directory 209) (with newer version of tar, you can gzip the tar file in a single command) % tar czf 209.tar.gz 209 (now, untar/restore the tar file back to the file system) % tar xf 209.tar -OR- % cat 209.tar | tar xf - (tar takes input from stdin) -OR- % tar xzf 209.tar.gzSee the man page for more information.
% wc myfile (output number of chars, words and lines) % wc -c myfile (output number of characters only) % wc -l myfile (output number of lines only) % wc -w myfile (output number of words only)
% finger | grep -v ^Login | awk '{ print $1}' | sort -u a209witt a209zhou a2501pyk a270cost a270razv a324anisNext example prints the names of people who are currently online. All records are sorted and are unique.
% finger | grep -v ^Login | cut -b10-34 | sort -u Angela Glinos Peng Yi ordinary user xiaoyu gao J. T. C. Chiu Lok Wilson