[Plain] www.2cto.com AWK operator ----------------------------------------------------------- = + = * ==%= ^ = assignment operator in the awk language under AIX? Conditional expression operator | &&! And, and, not <==!> Relational operators +-*/% ^ Arithmetic Operators + + -- prefix and suffix ------------------------------------------------ 1. set the name of the input field to the field variable awk '{name = $1; color = $4; if (color ~ /Yello/) print name "" is color "color} 'grade.txt P. bunny is color Yello 2. assign value comparison. Generally, assign a value to a in the BEGIN section. assign awk 'in in {BASELINE = "27"} {if ($6 <BASELINE) print $0} 'grade.txt J. To the variable name in BEGIN. lulu 06/99 48317 green 9 24 26 J. troll 07/99 4842 Brown-3 12 26 26 B. use the actual value awk '{if ($6 <27) print $0}' grade.txt J. lulu 06/99 48317 green 9 24 26 J. troll 07/99 4842 Brown-3 12 26 26 3. modify the value of the value awk '{if ($1 = "M. tansley ") $5 = $6-1; print $1, $6, $5, $7} 'grade.txt M. tansley 40 39 44 J. lulu 24 9 26 P. bunny 35 12 28 J. troll 26 12 26 L. tansley 30 12 28 4. modify the text field awk '{if ($1 = "J. troll ") ($1 =" J. troll. L "); print $1} 'grade.txt M. tansley J. lulu P. bunny J. troll. l. tansley 5. only the modified records awk '{if ($1 = "J. troll ") {$1 =" J. troll. L "; print $1} 'grade.txt awk: Syntax error at line 1 of program <{if ($1 =" J. troll "...> context is {if ($1 = "J. troll ") {$1 =" J. troll. L "; print >>1 1} <1 extra {awk: illegal statement at line 1 of program <{if ($1 =" J. troll "...> error cause: An error occurred while ending the awk '{if ($1 = "J. troll ") {$1 =" J. troll. L "; print $1} 'grade.txt J. troll. L 6. create a new domain awk 'in in {print "Name \ t Difference"} {if ($6 <$7) {$8 = $7-$6; print $1, $8} 'grade.txt Name Difference M. tansley 4 J. lulu 2 7. statistical data awk '(tot + = $6); END {print "Club student total points:" tot}' grade.txt M. tansley 05/99 48311 Green 8 40 44 J. lulu 06/99 48317 green 9 24 26 P. bunny 02/99 48 Yello 12 35 28 J. troll 07/99 4842 Brown-3 12 26 26 L. tansley 05/99 4712 Brown-2 12 30 28 Club student total points: 155 8. file length addition ls-l | awk '/^ [^ d]/{print $9 "\ t" $5} {tot + = $5} END {print "total KB: "tot} 'cat 0 cat_file.txt 28 data. f 356 delete_me_and_die 284 first2 61 grade.txt 235 info.txt 354 myfile 23 test. bak 225 test. SQL 225 who. out 1998 wow 229 total KB: 4018 view all files in the current directory: ls-l total 24-rw-r -- 1 xxxx group 0 Nov 19 cat-rw-r -- 1 xxxx group 28 Nov 14 cat_file.txt-rw-r -- r -- 1 xxxx group 356 Nov 16 data. f-rw-r -- 1 xxxx group 284 Nov 19 delete_me_and_die-rwxr -- r -- 1 xxxx group 61 Nov 8 first2-rw-r -- 1 xxxx group 235 nov 19 grade.txt-rwxr -- r -- 1 xxxx group 354 Nov 17 info.txt-rwxr ----- 1 xxxx group 23 Nov 7 myfile-rwxr -- r -- 1 xxxx group 225 Nov 15 test. bak-rwxr -- r -- 1 xxxx group 225 Nov 15 test. SQL-rw-r -- 1 xxxx group 1998 Nov 15 who. out-rw-r -- 1 xxxx group 229 Nov 19 14:44 wow -- the end --