awk & gawk command

2 분 소요

linux의 awk 명령어는 GNU 버전의 gawk로 심볼릭 링크되어 있습니다.

awk는 최초 기능을 만든 Aho + Weinberger + Kernighan(A:Alfred V. Aho, W:Peter J. Weinberger, K:Brian W. Kernighan) 3명의 이니셜을 조합하여 만든 이름 입니다.

표준 입력에 의한 데이터를 조작하거나 데이터화하는 용도로 사용하는 명령어 입니다.

기본 사용법

표준 입력 또는 파일을 읽어들여 ‘‘로 둘러싸인 패턴을 읽어 들여 공백 또는 탭을 기준으로 파싱해서 $1 부터 시작하는 변수를 만들어 줍니다.

참고로 $0은 입력된 모든 값을 가지고 있습니다.

$> ls -l | awk '{print $0}'
drwxr-xr-x     - geeksaga  7 May 11:56 Desktop
drwxr-xr-x     - geeksaga  6 May 14:03 Documents
drwxrwxr-x     - geeksaga 16 Jan 11:42 dotfiles
drwxr-xr-x     - geeksaga  6 Aug 10:33 Downloads
drwxrwxr-x     - geeksaga 29 Jul 10:54 geeksaga
drwxrwxr-x     - geeksaga  5 Apr  2019 node_modules
drwxr-xr-x     - geeksaga 14 Mar  2018 Public
drwxr-xr-x     - geeksaga 14 Mar  2018 Templates

$> ls -l | awk '{print $1}'
drwxr-xr-x
drwxr-xr-x
drwxrwxr-x
drwxr-xr-x
drwxrwxr-x
drwxrwxr-x
drwxr-xr-x
drwxr-xr-x

.$> ls -l | awk '{print $1, $7}'
drwxr-xr-x Desktop
drwxr-xr-x Documents
drwxrwxr-x dotfiles
drwxr-xr-x Downloads
drwxrwxr-x geeksaga
drwxrwxr-x node_modul
drwxr-xr-x Public
drwxr-xr-x Templates

help awk

$> 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: (standard)
	-f progfile		--file=progfile
	-F fs			--field-separator=fs
	-v var=val		--assign=var=val
Short options:		GNU long options: (extensions)
	-b			--characters-as-bytes
	-c			--traditional
	-C			--copyright
	-d[file]		--dump-variables[=file]
	-D[file]		--debug[=file]
	-e 'program-text'	--source='program-text'
	-E file			--exec=file
	-g			--gen-pot
	-h			--help
	-i includefile		--include=includefile
	-l library		--load=library
	-L[fatal|invalid]	--lint[=fatal|invalid]
	-M			--bignum
	-N			--use-lc-numeric
	-n			--non-decimal-data
	-o[file]		--pretty-print[=file]
	-O			--optimize
	-p[file]		--profile[=file]
	-P			--posix
	-r			--re-interval
	-S			--sandbox
	-t			--lint-old
	-V			--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

tldr awk

$> tldr awk

  awk

  A versatile programming language for working on files.
  More information: https://github.com/onetrueawk/awk.

  - Print the fifth column (a.k.a. field) in a space-separated file:
    awk '{print $5}' filename

  - Print the second column of the lines containing "something" in a space-separated file:
    awk '/something/ {print $2}' filename

  - Print the last column of each line in a file, using a comma (instead of space) as a field separator:
    awk -F ',' '{print $NF}' filename

  - Sum the values in the first column of a file and print the total:
    awk '{s+=$1} END {print s}' filename

  - Sum the values in the first column and pretty-print the values and then the total:
    awk '{s+=$1; print $1} END {print "--------"; print s}' filename

  - Print every third line starting from the first line:
    awk 'NR%3==1' filename