140 likes | 459 Views
awk. awk [- F r egexp ] program [ arg … ] or awk [- F r egexp ]-f progfile [ arg … ] -F defines field separator -f read program from file progfile instead on command line. awk program structure. BEGIN{ …. } action performed only once at beginning
E N D
awk awk [-Fregexp]program [arg …] or awk [-Fregexp]-f progfile [arg …] -F defines field separator -f read program from file progfile instead on command line
awk program structure BEGIN{ …. } action performed only once at beginning END{ … } action performed only at end after eof has been encountered. { … } group of actions to be performed for each input line pattern{ … } perform group of actions for each line that satisfies pattern. $1, $2, $3,… refer to fields on a line $0 refers to entire line Examples of pattern: regular expression delimited by / / or arithmetic expression like $3 > 0
Example: Print name and balance File Customer 78902:67.89:Henry Bunyan 99078:782.90:Boxcar Bertha 12345:450.00:Happy Jack 77777:78.99:Lucky Linda
% awk -F: '{print $3, "\t\$" $2}' customer Henry Bunyan $67.89 Boxcar Bertha $782.90 Happy Jack $450.00 Lucky Linda $78.99
If no field separator is specified default is white space Example % finger Login Name TTY Idle When Office chenjw Jianwei Chen p0 Thu 23:27 bermanka Kenneth Berman p2 Tue 07:03 hughlel Lenny Hughley *q1 3d Wed 09:52 guptaak Amit Gupta qd 19d Wed 10:55 % finger | awk '{print $3, $2, $4}' TTY Name Idle Chen Jianwei p0 Berman Kenneth p2 Hughley Lenny *q1 Gupta Amit qd
Example: Preparing an index file File Index1 Unix 1 C++ 99 sed 2 awk 26 Unix 53 Oz 7 sed 14 cut 43 Unix 10 sed 8 C++ 14 awk 88
File Index2 awk 101 emacs 26 vi 23 sed 89 awk 6 pipe 5 filter 4 cut 29 emacs 199 Oz 12
Merged index file awk 6,26,88,101 C++ 14,99 cut 29,43 emacs 26,199 filter 4 Oz 7,12 pipe 5 sed 2,8,14,89 Unix 1,10,53 vi 23
Creating index file index.final by combining index1 and index2 sort +0f -1 +0 -1 +1n index1 index2 > index.sorted awk -f index.awk index.sorted > index.final
index.awk BEGIN { i = 0; } $1 == pre { printf(",%s", $2); } $1 != pre { if (i > 0) { printf("\n%s\t%s",$1,$2); } else { printf("%s\t%s",$1,$2); i = 1; } pre = $1; } END { printf("\n"); }
Saving and distributing of files • Saving of a file • tar cvf filename.tarname1 name2 .. • compress filename.tar • Generated file with name filename.tar.z • To restore • uncompressfilename.tar.z • tarfilename.tar • Will restore entire hierarchy of directories name1, name2
Interactive use of the Shell • Prompt for the Bourne shell family $ • sh Bourne, AT&T mid 70’s • ksh Korn, AT&T 1988 and 1993 • bash Bourne again Shell Free Software Foundation 1989 • zsh Falstad , Princeton 1990 • Prompt for C shell family % • csh Berkeley early 80’s • tcsh Carnegie Mellon, OSU mid 80’s
What does a shell do? Do forever { Display prompt Read input until ^M Perform any substitutions Execute command Wait until command sends FINISH signal } Shell will terminate until one of the following commands is received: logout, login or ^D (and ignoreeof has not been set)
Starting a shell • Shells are kept in directory /bin/ • To start a new shell enter /bin/sh, or /bin/csh, or /bin/ksh etc. • The file /etc/passwd determines with which shell your session starts up. (On OZ default /usr/local/profile), compiled version of /usr/local/profile.c • The shells read (and execute) certain files with special names: • The C-shell uses .cshrc, .login and .logout • The Bourne shell uses .profile