gawk 一些雜類知識
gnu awk (gawk) 有一個環境變量, AWKPATH
是用來給用戶加入自定函數的路徑,如果不加指定,預設是
/usr/lib/awk
用戶可在 ~/.bash_profile 加入自己的, 那就可在命令列調用
我的如是
复制内容到剪贴板
代码:
fang@bash ~
$ tail -n1 < .bash_profile
export AWKPATH="/usr/lib/awk:/usr/share/awk:$HOME/lib/awk"
調用
复制内容到剪贴板
代码:
fang@bash ~
$ ls /usr/share/awk
assert.awk ctime.awk gettime.awk libintl.awk ord.awk rewind.awk zerofile.awk
bits2str.awk ftrans.awk group.awk nextfile.awk passwd.awk寫的 round.awk
cliff_rand.awk getopt.awk join.awk noassign.awk readable.awk strtonum.awk
fang@bash ~
$ gawk -f round.awk --source 'BEGIN{print round(3.6)}'
4
fang@bash ~
$ gawk -f round.awk --source 'BEGIN{print round(3.23)}'
3
在 script 調用則要用上一個由 gawk maintainer , Arnold Robbins 寫的 shell script, igawk
复制内容到剪贴板
代码:
IGAWK(1) Utility Commands IGAWK(1)
NAME
igawk - gawk with include files
SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ...
igawk [ all gawk options ] [ -- ] program-text file ...
DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to
gawk(1).
AWK programs for igawk are the same as for gawk, except that, in addition, you may
have lines like
@include getopt.awk
in your program to include the file getopt.awk from either the current directory or
one of the other directories in the search path.
OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk
supports.
EXAMPLES
cat << EOF > test.awk
@include getopt.awk
BEGIN {
while (getopt(ARGC, ARGV, "am:q") != -1)
...
}
EOF
igawk -f test.awk
fang@bash ~
$ cat round_num
#! /bin/igawk -f
@include round.awk
{
for (i=1; i<=NF; i++){
print round($i)
}
}
fang@bash ~
$ echo 12.35 4.6 23.12 | ./round_num
12
5
23