next up previous contents
Next: awk 範例一 Up: awk 工具 Previous: awk 工具   Contents

awk 命令簡介

  1. awk 相較於 sed 作用於一整個行的處理,awk 是『以行為一次處理的單位』, 而『以欄位為最小的處理單位』。awk 命令輔助說明如下:
    [dywang@dywmac zzz]$ awk --help
    Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
    Usage: awk [POSIX or GNU style options] [--] 'program' file ...
    POSIX options:		GNU long options:
    	-f progfile		--file=progfile
    	-F fs			--field-separator=fs
    	-v var=val		--assign=var=val
    	-m[fr] val
    	-O			--optimize
    	-W compat		--compat
    	-W copyleft		--copyleft
    	-W copyright		--copyright
    	-W dump-variables[=file]	--dump-variables[=file]
    	-W exec=file		--exec=file
    	-W gen-po		--gen-po
    	-W help			--help
    	-W lint[=fatal]		--lint[=fatal]
    	-W lint-old		--lint-old
    	-W non-decimal-data	--non-decimal-data
    	-W profile[=file]	--profile[=file]
    	-W posix		--posix
    	-W re-interval		--re-interval
    	-W source=program-text	--source=program-text
    	-W traditional		--traditional
    	-W usage		--usage
    	-W use-lc-numeric	--use-lc-numeric
    	-W version		--version
    
    To report bugs, see node `Bugs' in `gawk.info', which is
    section `Reporting Problems and Bugs' in the printed version.
    
    gawk is a pattern scanning and processing language.
    By default it reads standard input and writes standard output.
    
    Examples:
    	gawk '{ sum += $1 }; END { print sum }' file
    	gawk -F: '{ print $1 }' /etc/passwd
    
  2. awk --dump-variables dump 所有變數,指定的檔案空字串,則預設存在 awkvars.out,其中 FS: string (" ") 表示欄位分隔符號為空白 " "
    [dywang@dywmac ~]$ awk --dump-variables ''
    [dywang@dywmac ~]$ cat awkvars.out 
    ARGC: number (1)
    ARGIND: number (0)
    ARGV: array, 1 elements
    BINMODE: number (0)
    CONVFMT: string ("%.6g")
    ERRNO: number (0)
    FIELDWIDTHS: string ("")
    FILENAME: string ("")
    FNR: number (0)
    FS: string (" ")
    IGNORECASE: number (0)
    LINT: number (0)
    NF: number (0)
    NR: number (0)
    OFMT: string ("%.6g")
    OFS: string (" ")
    ORS: string ("\n")
    RLENGTH: number (0)
    RS: string ("\n")
    RSTART: number (0)
    RT: string ("")
    SUBSEP: string ("\034")
    TEXTDOMAIN: string ("messages")
    
  3. 查看 awk 命令連結到 gwak,也就是在 Linux 上 awk 是 gawk。
    [dywang@dywmac zzz]$ ll /usr/bin/awk 
    lrwxrwxrwx. 1 root root 14 Jun  2  2018 /usr/bin/awk -> ../../bin/gawk
    
  4. gawk --version (GNU Awk) 版本宣告,是自由軟體。
    [dywang@dywmac zzz]$ gawk -W version
    GNU Awk 3.1.7
    Copyright (C) 1989, 1991-2009 Free Software Foundation.
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program. If not, see http://www.gnu.org/licenses/.
    



De-Yu Wang 2019-08-23