awksed를 이용한 awk sed 실무예제 1

Contents

1 특별한 의미를 가지는 변수들
2 examples
2.1 server 라는 단어가 들어가는 프로세스들 한꺼번에 죽이기
2.2 /etc/passwd 파일에서 uid만 리스팅
2.3 정규식 사용
2.4 파일앞에 레코드넘버(NR:Number of Record) 붙여서 출력하기

1 특별한 의미를 가지는 변수들 #

FS    : 필드 구분자(정규식), 기본값은 공백(탭포함) -F 옵션으로 지정가능 (예: -F":")

NF    : 현재 레코드에서의 필드 개수

NR                The ordinal number of the current record from
				 the start of input. Inside a BEGIN action the
				 value is zero. Inside an END action the value
				 is the number of the last record processed.

FNR               The ordinal number of the current record in the
				 current file. Inside a BEGIN action the value
				 is zero. Inside an END action the value is the
				 number of the last record processed in the last
				 file processed.

FILENAME          A pathname of the current input file.

RS                The input record separator; a newline character
				 by default.

OFS               The print statement output field separator; a
				 space character by default.

ORS               The print statement output record separator; a
				 newline character by default.

OFMT              Output format for numbers (default %.6g).  If
				 the value of OFMT is not a floating-point
				 format specification, the results are
				 unspecified.

CONVFMT           Internal conversion format for numbers (default
				 %.6g).  If the value of CONVFMT is not a
				 floating-point format specification, the
				 results are unspecified.

SUBSEP            The subscript separator string for multi-
				 dimensional arrays; the default value is "\034"

ARGC              The number of elements in the ARGV array.





ARGV              An array of command line arguments, excluding
				 options and the program argument numbered from
				 zero to ARGC-1.

				 The arguments in ARGV can be modified or added
				 to; ARGC can be altered. As each input file
				 ends, awk will treat the next non-null element
				 of ARGV, up to the current value of ARGC-1,
				 inclusive, as the name of the next input file.
				 Thus, setting an element of ARGV to null means
				 that it will not be treated as an input file.
				 The name - indicates the standard input. If an
				 argument matches the format of an assignment
				 operand, this argument will be treated as an
				 assignment rather than a file argument.

ENVIRON           Array of environment variables; subscripts are
				 names.  For example, if environment variable
				 V=thing, ENVIRON["V"] produces thing.

RSTART            The starting position of the string matched by
				 the match function, numbering from 1. This is
				 always equivalent to the return value of the
				 match function.

RLENGTH           The length of the string matched by the match
				 function.

2 examples #

2.1 server 라는 단어가 들어가는 프로세스들 한꺼번에 죽이기 #

$ ps -ef | grep 'server' | awk '{print $2}' | xargs kill -9

2.2 /etc/passwd 파일에서 uid만 리스팅 #

$ awk -F":" '{print $3}' /etc/passwd

2.3 정규식 사용 #

$ awk '/user[12][0-9]/ {print $0}' /my_file

2.4 파일앞에 레코드넘버(NR:Number of Record) 붙여서 출력하기 #

$ awk '{print NR "\t" $0}' /etc/passwd
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2013-11-18 18:04:37
Processing time 0.0101 sec