1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| 1. Numeric int(x) sqrt(x) rand(): return a random number, [0-1) srand([expr]): use expr as a seed for random generator, if not provided, use system current time
# awk 'BEGIN{print int(2.3), int(012), int(0xFF), int(3a), int(a3)}' 2 10 255 3 0
# awk 'BEGIN{print rand(), 10*rand()}'
# awk 'BEGIN{srand(); print rand(), 10*rand()}'
2. String sub(regex, replacement[, target]): use the replacement to replace the regex matched in string target(by default $0) gsub(regex, replacement[, target]): global sub
gensub(regex, replacement, how[, target]): gawk gensub(regex, replacement, "g|G" target) => gsub gensub(regex, replacement, 0, target) => gsub, but with warning gensub(regex, replacement, N, target) => N is a digit from 1 to 9, index of the matched sub-expression
# awk 'BEGIN{info="this is a test2010test!"; gsub(/[0-9]+/,info); print info}'
# gawk 'BEGIN{a="abc def"; b=gensub(/(.+) (.+)/, "\\2 \\1", "g", a); print b}' def abc
# echo "a b c a b c" | gawk '{print gensub(/a/,"AA",2)}' a b c AA b c
index(string, find): index of the find in the string, or 0 if not present match(string, regex[, array]): position of the regex occurring in the string
length([string]) substr(string, position[, len]) split(string, array[, regexp]): split the string into the array on the regex
# awk 'BEGIN{s="this is a test"; print index(s, "a")}'
# awk 'BEGIN{s="this is a test"; print index(s, "a") ? "ok" : :no found"}'
# awk 'BEGIN{s="this is a match test"; pos=match(s, /m[a-z]+/, array); print pos; for(i in array) print i, array[i]}' 11 0start 11 0length 5 0 match
# awk 'BEGIN{s="this is a test"; print substr(s, 9, 1)}' a
# awk 'BEGIN{s="this is a test"; print substr(s, 9)}' a test
# awk 'BEGIN{s="this is a split test"; len=split(s,array); print len; for(i in array) print i, array[i]}' 5 4 split 5 test 1 this 2 is 3 a
# awk 'BEGIN{FS=":"}/^root/{split($0,array); for(i in array) print i, array[i]}' /etc/passwd # awk '/^root/{split($0,array,/:/); for(i in array) print i, array[i]}' /etc/passwd 4 0 5 root 6 /root 7 /bin/bash 1 root 2 x 3 0
Associative Arrays a. sorting by values len = asort(s) # s: changed, the indexes are replaced with sequential integers len = asort(s, d) # s: unchanged; d: a sorted duplicate array of s
b. sorting by indexes len = asorti(s) # s: changed, restorted by indexes len = asorti(s, d) # s: unchanged; d: a new array of sorted indexes
# awk '{a[$1]=$2}END{for(i in a) print i, a[i]}' abc.txt 10 35 12 30 22 13 24 20
# awk '{a[$1]=$2}END{for(i=1;i<=asort(a,b);i++) print i, b[i]}' abc.txt 1 13 2 20 3 30 4 40
# awk '{a[$1]=$2}END{for(i=1;i<=asorti(a,b);i++) print i, b[i]}' abc.txt 1 10 2 12 3 22 4 24
sprintf(format, expr): return the printed expr according to the format tolower(string) toupper(string)
# awk 'BEGIN{s=sprintf("%.2g %s %d", 3.1415926, 3.1415926, 3.1415926); print s}' 3.1 3.14159 3
3. Time mktime("YYYY MM DD HH MM SS[ DST]"): return a time stamp systime(): return current time stmap strftime([format[, ts])
# awk 'BEGIN{print mktime("2014 12 20 14 25 32")}' # awk 'BEGIN{print systime()}' # awk 'BEGIN{print strftime()}' # awk 'BEGIN{print strftime("%c", systime())}' # date +%c
4. IO close(file[, how]): close file, pipe or co-process; how is either "from" or "to"
getline set $0 from next input record, set NF, NR, FNR getline <file set $0 from next record of file, set NF getline var set var from next input record, set NR, FNR getline var <file set vat from next record of file
command | getline [var] run command piping the output either into $0 or var command | & getline [var] run command as a co-process piping the output either into $0 or var. co-processes are a gawk extension
next: stop processing the current input record
print [expr-list [>file] ] printf [format, expr-list [>file] ]
system("cmd") execute the command, and return the exit status
fflush([file]) flush any buffers
print ... | command write on a pipe print ... |& command write on a co-process
# awk 'BEGIN{while("cat /etc/passwd" | getline) print; close("/etc/passwd")}' # awk 'BEGIN{while(getline <"/etc/passwd") print; close("/etc/passwd")}'
# awk 'BEGIN{"date" | getline d; print d}' # awk 'BEGIN{"date" | getline d; split(d,mon); print mon[2]}'
# awk 'BEGIN{while("ls" | getline) print}'
# awk 'BEGIN{printf("Enter your account: "); getline name; print name}' awk 'BEGIN{l=system("ls -l"); print l}'
// prompting, wait for input # awk 'BEGIN{printf "What is your name? "; getline name <"/dev/tty"} $1~name {print "Found" name "on line ", NR".} END{print "See you," name "."}' /etc/passwd
// count number of file # awk 'BEGIN{while(getline <"/etc/passwd" >0) lc++; print lc}
// sort # awk '{print $1, $2 | "sort"} END{close("sort)}' abc.txt
|