linux/Tip

awk -추가

efrit 2007. 3. 28. 12:26

http://blog.naver.com/brightest?Redirect=Log&logNo=150015288879

http://www.gnu.org/software/gawk/manual/gawk.html

1. 라인선택 후 필드 출력
awk '/dairy/ {print $3, $1}' foot
; 파일 food의 레코드들 중에서 문자열 dairy를 갖는 레코드 선택 후,
선택된 레코드의 3번째 필드와 1번째 필드 출력
필드와 필드사이는 공백으로 출력


2. 암호 레코드 찾은 후 패스워드
grep $USER /etc/password

; 자신의 암호 레코드를 찾는다.
awk -F: '{print $1, $3, $4}' /etc/passwd |more
awk -F: '$1 == "root" {print $1, $3}' /etc/passwd


3. 필드 구분자 사용
awk -F: '{print $2}' aaa.txt
; 필드구분자를 :으로 하고, 두번째 필드를 출력


4.변수와 문자열 구분하기

awk -v item='Grocery Item' '{print item, $1}' gdbase

; v는 변수를 정의할 것임을 알려준다.


5. 정규표현으로 레코드 선택하기

awk -F: '/^[a-m][a-z]*:/ {print $1, $7}' /etc/passwd

awk '/[Vv]eg/ {print $0}' gdbase

; 레코드 한줄을 출력


6.
who | awk ' {print $1}' | sort -u

       sort - sort lines of text files
       -d, --dictionary-order
             consider only blanks and alphanumeric characters
       -f, --ignore-case
              fold lower case to upper case characters
       -g, --general-numeric-sort
              compare according to general numerical value
       -i, --ignore-nonprinting
              consider only printable characters
       -M, --month-sort
              compare (unknown) < `JAN' < ... < `DEC'
       -n, --numeric-sort
              compare according to string numerical value
---------------------
du -a|awk '{print $2}'
who| awk '{ print $1, $5 }'
---------------------
< awk Built-in Variables >
FILENAME ;
FS  ; field separator character (default blank & tab)
NF  ; number of fields in input record
NR  ; number of input record
OFMT  ; output format for numbers (default %g; see printf(3))
OFS  ; output field separator string (default blank)
ORS  ; output record separator string (default newline)
RS  ; inupt record separator character (default newline)

---------------------

< Shell Built-in Variables >

$# ; the number of arguments
$* ; all arguments to shell
$@ ; similar to $*
$- ; options supplied to the shell
$? ; return value of the last command executed
$$ ; process-id of the shell
$! ; process-id of the last command started with &
$HOME ; default argument for cd command
$IFS ; list of characters that separate words in arguments
$MAIL ; file that, when changed, triggers "you have mail" message
$PATH ; list of directories to search for commands
$PS1 ; prompt string, defalut '$ '
$PS2 ; prompt string for continues command line, default '> '

---------------------
chmod +x $1
chmod +x $*
--------------------

grep "$*" /usr/you/lib/phone-book

> grep dow jones
...

grep -y pattern ; case-independent.

----------------------

2>&1;  stderr 을 stdout에 더함.
ex)

> /usr/bin/time wc ch3.1 >wc.out 2>&1
/usr/bin/time ls >test  2>&1

1>&2; stdout 을 stderr에 더함.

혹은

#!/usr/bin/sh 안에서 수행


=========================================================

#!/usr/bin/awk
#

BEGIN {
  max = 0
}
/^#define/ {
  print $0
  if( max<$3 ) max = $3
}
END {
  printf "#define %-29s %4d\n", "COMMENT",      max+1
  printf "#define %-29s %4d\n", "SPACES",       max+2
}