C2110 UNIX and programming Lesson 12 -1C2110 UNIX and programming Petr Kulhanek kulhanek@chemi.muni.cz National Center for Biomolecular Research, Faculty of Science Masaryk University, Kamenice 5, CZ-62500 Brno Lesson 12 PS / 2020 Distance form of teaching: Rev2 C2110 UNIX and programming Lesson 12 -2Summary L10 ➢ AWK C2110 UNIX and programming Lesson 12 -3The process of executing the script open file? Yes No parse record (split into words) execute the block initialization S BEGIN { } { } /PATTERN/ { } END { } 1 2 3 4 finalization load record (row)? No Yes execute the block record matches PATTER? Yes No E user scriptlogic of AWK interpreter variables are global (unless specified otherwise) script.awk C2110 UNIX and programming Lesson 12 -4Starting AWK scripts Text file processing: $ awk –f script.awk input1.txt input2.txt $ awk –f script.awk < vstup.txt $ cat soubor.txt | awk –f script.awk awk script analyzed text files result is printed on the screen Analyzed data can be sent via standard input: language interpreter Indirect start: C2110 UNIX and programming Lesson 12 -5- Variables Special variables: NF number of fields in the current record (Number of Fields) NR order of processed record (Number of Records) $0 whole record $1, $2, $3 ... individual record fields Variable value: print A + C; print B; Assignment to a variable: A = 10; B = "this is a text " C = 10.4567; D = A + C; character $ allows programmatic access to individual fields of the record Example: for(i=1; i < NF; i++){ sum += $i; } C2110 UNIX and programming Lesson 12 -6Next Subject ➢ AWK C2110 UNIX and programming Lesson 12 -7- Obsah ➢ AWK • Conditions, logical operations • Run control (next, exit) • Loops • Arrays