Upgrade to Pro — share decks privately, control downloads, hide ads and more …

awk

Avatar for jiangbo jiangbo
August 23, 2012

 awk

simple introduction of awk

Avatar for jiangbo

jiangbo

August 23, 2012

More Decks by jiangbo

Other Decks in Programming

Transcript

  1.  始祖 : Aho, Weinberger, and Kernighan  AWK is

    a programming language designed for processing text-based data, either in files or data streams. ——wikipedia  版本  awk, nawk, mawk, pgawk, …  GNU awk: gawk 3
  2. 5

  3.  awk [options] ‘script’ file(s)  awk [options] –f scriptfile

    file(s) Options: -F to change input field separator -f to name script file 6
  4.  构成 : pattern {action}  缺 pattern ,对所有行执行 action

     缺 action ,打印所有匹配行  两个都没有?——逗我玩呢! Example: awk '/for/' testfile awk ‘{print}’ testfile CSCI 330 - The UNIX System 7
  5.  awk 运行过程 :  读取:按行浏览文件( Line by line )

     切分:将每个输入行切分为多个域( Field )  匹配:根据 pattern 进行行或域匹配  执行:对匹配行执行操作 CSCI 330 - The UNIX System 8
  6.  域 (Field): 每一行的基础单元  分隔符 (field separator):  default

    field separator is whitespace  记录 (record): is the collection of fields in a line  A data file is made up of records CSCI 330 - The UNIX System 9
  7.  AWK 支持两类 buffer: record and field  field buffer:

     当前记录中的每一个 field  表示 : $1, $2, …  record buffer :  $0 表示整条记录 11 我是 zero
  8. FS Field separator (default=whitespace) RS Record separator (default=\n) NF Number

    of fields in current record NR Number of the current record OFS Output field separator (default=space) ORS Output record separator (default=\n) 12
  9. 14

  10. 15

  11.  match  entire input record regular expression enclosed by

    ‘/’s  explicit pattern-matching expressions ~ (match), !~ (not match)  expression operators  算术  关系  逻辑 16
  12. OperatorMeaning Example + Add x + y - Subtract x

    – y * Multiply x * y / Divide x / y % Modulus x % y ^ Exponential x ^ y Example: % awk '$3 * $4 > 500 {print $0}' file 17
  13. Operator Meaning Example < Less than x < y <

    = Less than or equal x < = y == Equal to x == y != Not equal to x != y > Greater than x > y > = Greater than or equal to x > = y 18
  14. Operator Meaning Example && Logical AND a && b ||

    Logical OR a || b ! NOT ! a Examples: % awk '($2 > 5) && ($2 <= 15) {print $0}' file % awk '$3 == 100 || $4 > 50' file CSCI 330 - The UNIX System 19
  15.  匹配连续输入行中的一个区域 语法 : pattern1 , pattern2 {action}  pattern

    可以为任何 simple pattern  pattern1 开始执行 action  pattern2 结束执行 action 20
  16. 21

  17. 22

  18. = assign result of right-hand-side expression to left-hand-side variable ++

    Add 1 to variable -- Subtract 1 from variable += Assign result of addition -= Assign result of subtraction *= Assign result of multiplication /= Assign result of division %= Assign result of modulo ^= Assign result of exponentiation 25
  19.  Print output goes to standard output unless redirected via:

    > “file” >> “file” | “command”  will open file or command only once  subsequent redirections append to already open stream CSCI 330 - The UNIX System 27
  20. 语法 : printf(format-string, var1, var2, …)  works like C

    printf  each format specifier in “format-string” requires argument of matching type CSCI 330 - The UNIX System 28
  21. %d, %i decimal integer %c single character %s string of

    characters %f floating point number %o octal number %x hexadecimal number %e scientific floating point notation %% the letter “%” CSCI 330 - The UNIX System 29
  22. CSCI 330 - The UNIX System 30 Given: x =

    ‘A’, y = 15, z = 2.3, and $1 = Bob Smith Printf Format Specifier What it Does %c printf("The character is %c \n", x) output: The character is A %d printf("The boy is %d years old \n", y) output: The boy is 15 years old %s printf("My name is %s \n", $1) output: My name is Bob Smith %f printf("z is %5.3f \n", z) output: z is 2.300
  23.  between “%” and letter %10s %7d %10.4f %-20s 

    meaning:  width of field, field is printed right justified  precision: number of digits after decimal point CSCI 330 - The UNIX System 31
  24. Syntax: sprintf(format-string, var1, var2, …)  Works like printf, but

    does not produce output  Instead it returns formatted string Example: { text = sprintf("1: %d – 2: %d", $1, $2) print text } CSCI 330 - The UNIX System 32
  25. split(string, array, fieldsep)  divides string into pieces separated by

    fieldsep, and stores the pieces in array  if the fieldsep is omitted, the value of FS is used. Example: split("auto-da-fe", a, "-")  sets the contents of the array a as follows: a[1] = "auto" a[2] = "da" CSCI 330 - The UNIX System 34
  26. Syntax: arrayName[index] = value Examples: list[1] = "one" list[2] =

    "three" list["other"] = "oh my !" CSCI 330 - The UNIX System 36
  27.  The delete function can be used to delete an

    element from an array. Format: delete array_name [index] Example: delete deptSales["supplies"] CSCI 330 - The UNIX System 37
  28. 条件  if-else 循环  for ▪ with counter ▪

    with array index  while  do-while  also: break, continue CSCI 330 - The UNIX System 38
  29. Syntax: if (conditional expression) statement-1 else statement-2 Example: if (

    NR < 3 ) print $2 else print $3 CSCI 330 - The UNIX System 39
  30. Syntax: for (initialization; limit-test; update) statement Example: for (i =

    1; i <= NR; i++) { total += $i count++ } CSCI 330 - The UNIX System 40
  31. Syntax: for (var in array) statement Example: for (x in

    deptSales) { print x, deptSales[x] } CSCI 330 - The UNIX System 41
  32. Syntax: while (logical expression) statement Example: i = 1 while

    (i <= NF) { print i, $i i++ } CSCI 330 - The UNIX System 42
  33. Syntax: do statement while (condition) statement is executed at least

    once, even if condition is false at the beginning Example: i = 1 do { print $0 i++ } while (i <= 10) CSCI 330 - The UNIX System 43
  34.  break exits loop  continue skips rest of current

    iteration, continues with next iteration CSCI 330 - The UNIX System 44
  35. for (x = 0; x < 20; x++) { if

    ( array[x] > 100) continue printf "%d ", x if ( array[x] < 0 ) break } CSCI 330 - The UNIX System 45