- print the last field of records; print the last
nth
field with$(NF - n)
{ print $NF }
pipe
不能连续使用,只能有一个管道,而且管道的输出会直接去到标准输出,没有办法用变量收集array
, 实际上是类似于lua里面的table, 可以有数字键,也可以有字符串键
- awk in fact, inspires languages like
perl
andruby
- awk is Turing complete
- awk is dedicated for string processing
- A lot of implicit thing, like default
action
, defaultpattern
; it is because when awk is invented, keystrokes, in another word, IO resources is valuable. Many people share a small slice of CPU.
expression {action}
,expression
can beregex
pattern, also common seen expressions something likeNR == 2
- in fact,
regex
pattern is a shorthand for$0 ~ /regex/
- functions call in
awk
:()
can be omitted, inspiringruby
somehow, and parameter separator can be either<space>
or,
- there is
array
type in awk - it can load
module
with@load
- you can call other command line tools in awk, also
piping
with|
andredirecting
with>
printf
, just a C-likeprintf
sharing the same features- A lot of
C
math functions, likeatan2
,sqrt
,rand
NOTE: gsub
does not work as this picture claims, it acutally replace the 1st arg with the 2nd
# substitute all `root` for `good`
ps aux | awk ' { gsub("root" , "good", $0); print }'
# NOTE: 1 index
# this program print usernames without the 1st letter
ps aux | awk '{ a = substr($1, 2); print a}'