AWK

세부 기술 자료는

http://www.okpos.com/docs/gnu/gnudocs/gawk/gawk_toc.html

 

1. awk의 기본 개념

 

1) awk?

; awk란 이름은 이 유틸리티를 작성한 A.V.Aho, P.J. Weinberger, B. Kernigham의 머리글자를 따온 것

 awk는 일종의 프로그래밍 언어지만 일반적인 언어라기 보다는 주로 패턴의 검색과 조작을 주목적으로 만들어진 것이다.

② 파일의 각 라인에서 필드(field)를 인식할 수 있는 패턴 매칭 기능을 가지고 이들 필드를 자유자재로 조작 가능한 유틸리티를 작성하고자

만든 것이다.

 

2) awk의 응용분야

데이터 프로세싱리포트 작성간단한 데이터베이스 구축

 

3) awk를 이용한 작업

① 프로그래머가 자신이 작성한 프로그램의 입력 화일이 특정한 형식에 들어 맞게 이루어져 있는지 검사.

② 출력화일을 처리하여 리포트를 만들어 냄.

③ 다른 프로그램의 입력 형식에 맞게 변환하는 작업에 이용.

 

2. awk 프로그램의 구조 및 실행

 

(1) awk 프로그램의 구조

 

  1) awk ' pattern {action}

 

    pattern {action}

    .

    .

    .

    ' filenames <-----------------입력화일(예제 : students)

 

  2) awk -f parttern-action-file filenames <----- 입력화일

    awk실행 action을 가진 프로그램 file

 

(2) awk pattern

 

      패 턴 내 용

      BEGIN 입력화일을 읽어들이기 전에 옆에 제시되는 문자을 실행시키도록 한다.

      END awk가 모든 입력을 처리한 후옆에 제시되는 문장을 실행시키도록 한다.

      expression 식을 평가한 후 이 식이 참 non-zero이거나 non-null인 경우 문장을 실행한다.

      /re/ 규식과 일치하는 문자열을 포함하고 있는 라인에서 문장을 실행한다.

      compound-pattern복합패턴이라는 것으로 &&(and), ||(or) , !(not) 그리고 괄호에 의해 연결시킨 것이다.

      expression의 경우와 마찬가지로 복합 패턴도 참인 경우의 문장을 실행시킨다.

      pattern1, pattern2 이러한 패턴을 범위 패턴이라한다현재 처리되고 있는 라인이pattern1과 일치되고다음에

      따라오 는 라인 중 임의의 라인이 pattern2와 일치할 때범위 패턴은 두 라인 사이의 각 라인과 일치한다.

 

 

(3) awk의 연산자

      연 산 자 내용

      = += -= *= /= %= 배정(assignment)연산자

      + - * / % ++ -- 산술 연산자

      || && ! 논리 연산자(|| = OR, && = AND, ! = NOT)

      > >= < <= == != 비교 연산자

      v ~p 변수 V가 패턴 P에 부합되면 참

      v !~p 변수 V가 패턴 P에 부합되지 않으면 참

 

(4) 액션(Actions)

액션은 문장(statements)으로 이루어져 있다액션은 간단하게 상수 하나로 이루어질 수도 있고개행 문자나 세미콜론(;)에 의해 분리된

몇 개의 문장의 연속으로 구성될 수도 있다.

 

 

   expressions

   print expression-list

   printf(format, expression-list)

   if (expression) statement

   if (expression) statement else statement

   while (expression) statement

   for (expression; expression; expression) statement

   for (variable in array) statement

   do statement while (expression)

   break

   continue

   next

   exit

   exit expression

   {statement}

 

 

 

(5) awk에서 미리 정의된 몇가지 변수들 변 수 내 용

      FILENAME 현재 처리되고 있는 입력 파일의 이름

      FS 입력 필드 분리문자

      NR 현재 레코드()의 번호

      NF 현재 레코드()의 필드의 갯수

      OFS출력되는 필드의 분리문자

 

 

 

3. awk의 기본예제

(1) 예제 입력 파일 소개

  ① 입력화일의 이름은 students

  ② 이 파일의 각 라인은 3개의 필드로 구성(학생 성명학과명나이)

  ③ 각 필드는 공백에 의해서 분리(공백을 필드 분리자로 간주함.)

  < awk는 각 라인에서 필드를 추출해 내는 데 필드 분리자(field separator)를 사용필드 분리자는 보통 하나 이상의 공백

  문자이다.>

 

1) 입력화일 예제 students

 

  % cat students

  John,P Physics 20

  Rick,L Mechanical 21

  Jack,T electrical 23

  Larry,M Chemical 22

  Phil,R Electrical 21

  Mike,T mechanical 22

  Paul,R Chemical 23

  John,T Chemical 23

  Tony,N Chemical 22

  James,R Electrical 21

 

 

 1) (expression)에 맞는 field 프린트하기

  % awk '$3 > 22 {print $1}' students

  Jack,T

  Paul,R

  John,T

 

 

 

 2) if 문을 사용하여 조건에 맞는 line 분리하기(각 파일에 저장)

 

  step 1 : if문을 사용하는 프로그램을 awkprog1이라는 파일로 만든다.

 

    % cat awkprog1

    { if ($1 ~ /^J/) printf "%s\n", $0 > "Jfile"

    if ($1 ~ /^P/) printf "%s\n", $0 > "Pfile"}

 

  step 2 : students 입력화일에 awkpog1 프로그램 화일을 적용한다.

 

    % awk -f awkprog1 students

 

  step 3 : 결과 보기

    % cat Jfile

    John,P Physics 20

    Jack,T electrical 23

    John,T Chemical 23

    James,R Electrical 21

 

    % cat Pfile

    Phil,R Electrical 21

    Paul,R Chemical 23

 

 

 3) 평균값 구하기

 

  <프로그램 awkprog2, awkprog3>

 

  % cat awkprog2

  {sum += $3}

  END {printf "The average of the ages is %.2f\n", sum/NR}

 

  % cat awkprog3

  {sum += $3

  ++no}

  END {printf "The average of the ages is %.2f\n", sum/no}

 

  <결 과>

  % awk -f awkprog3 students

  The average of the ages is 21.80

 

 

 4) while  do문을 이용하여 평균값 구하기

 

  <프로그램 awkprog4>

  % cat awkprog4

  {if (NF > 0) {

  sum = 0

  n = 1

  while (n <= NF) {

  sum = sum + $n

  n = n+1

  }

  printf "Average is %d\n", sum/NF

  }

  else

  print}

 

  <예 제>

  % awk -f awkprog4 test

  Average is 17

  Average is 3

  Average is 25

  Average is 0

 

 

Getting Started

 

basic

 

Sample data:

Beth   4.00 0

Dan    3.75 0

Kathy  4.00 10

Mark   5.00 20

Mary   5.50 22

Susie  4.25 18

 

예제:

awk '$3 > 0 { print $1, $2, $3 }' awk.data

결과:

Kathy 4.00 10

Mark 5.00 20

Mary 5.50 22

Susie 4.25 18

 

예제:

awk '$3 > 0 { print $1, $2 * $3 }' awk.data

결과:

Kathy 40

Mark 100

Mary 121

Susie 76.5

 

 

중요:

$0 : line 전체

 

 

The structure of an AWK Program

 

pattern { action } . . . single quotes protects $ by shell and allows multi lines

 

awk '$3 == 0 { print $1 }' awk.data

awk '$3 == 0'              awk.data

awk         '{ print $1 }' awk.data

 

 

중요:

{ action } 생략  ->  default : { print $0 }

pattern 생략  ->  default : TRUE

 

 

Running an AWK program

awk 'program' file1 [file2 ...] awk 'program' # from stdin until end-of-file awk -f progfile input-files

 

Simple output

fileds, $1 $2 ..., a whole line $0

 

 

Printing every line { print } { print $0 }

 

Printing certain fields { print $1, $3}

 

중요:

built-in variable - 대문자로 표기

수치값은 모두 1부터 시작 1,2,3,...

 

 

NF : the number of fields { print NF, $1, $NF}

 

Computing and printing { print $1, $2 * $3 }

 

NR : the number of records; printing line number { print NR, $0}

 

Putting text in the output { print "total pay for", $1, "is", $2 * $3 }

 

중요:

print 문에서 ',' 를 사용하면 ' '(space)가 들어감

',' 가 없으면 앞에것에 붙어서 출력됨

 

Fancier output

 

printf(format, value1, value2 ...)

에서와 동일한 형식

 

 

예제:

{ printf("total pay for %s is $%.2f\n", $1, $2 * $3)}

 

결과:

total pay for Beth is $0.00

total pay for Dan is $0.00

total pay for Kathy is $40.00

total pay for Mark is $100.00

total pay for Mary is $121.00

total pay for Susie is $76.50

 

 

예제:

{ printf("%-8s $6.2f\n", $1, $2 * $3)}

 

결과:

Beth     $  0.00

Dan      $  0.00

Kathy    $ 40.00

Mark     $100.00

Mary     $121.00

Susie    $ 76.50

 

 

Sorting the output

awk '{printf(%6.2f %s\n", $2* $3, $0)}' awk.data | sort

 

Selection

 

finding interesting line $2 >= 5 $2 * $3 > 50 $1 == "Susie" /Susie/ # regular expression $2 >= 4 || $3 >= 20 !($2 < $4 && $3 < 20)

 

Data validation NF !=3 $2 < 3.35 $2 > 10

 

BEGIN and END

맨처음 (맨나중한번만 수행됨

 

 

예제:

BEGIN { print "NAME   RATE   HOURS "; print ""}

{print }

 

결과:

NAME   RATE   HOURS

 

Beth   4.00 0

Dan    3.75 0

Kathy  4.00 10

Mark   5.00 20

Mary   5.50 22

Susie  4.25 18

 

Computing with AWK

No declaration, 초기값은 0으로 자동 지정됨

 

 

Counting $3 > 15 { emp=emp+1 } END {print emp, " work more than 15 hours" }

 

Computing sum and average

 

예제:

{pay =pay +$2*$3}

END{

print NR, "employess"

print "total", pay

print "average", pay/NR}

 

결과:

6 employess

total 337.5

average 56.25

 

예제:

$2 > maxrate {

maxrate=$2

maxemp=$1 }

END{

print "max pay", maxrate, "for", maxemp

}

 

결과:

max pay 5.50 for Mary

 

 

String concatenation no neeed to be explicitly initialized

 

예제:

{names=names$1" " }

END{print names}

 

결과:

Beth Dan Kathy Mark Mary Susie

 

 

Built in fuction

예제:

{print $1, length($1)}

 

결과:

Beth 4

Dan 3

Kathy 5

Mark 4

Mary 4

Susie 5

 

예제:

{nc=nc+length($0)+1   # +1 CR를 위함

 nw=nw+NF

}

END {

print NR, "lines,", nw,"words,", nc,"char"}

 

결과:

6 lines, 18 words, 88 char   # wc 와 동일한 기능

 

Control-flow statements

 

if statement

 

예제:

$2 > 6{

n=n+1;

pay=pay+$2*$3}

END{

if(n>0)

   print n, "employess, total", pay,\

            "average", pay/n

else

   print "no employees"

}

 

결과:

no employees

 

 

While statement

예제:

#input amount rate years   # '#'  comment 

#output compounded values at the end of each year

{

i=1;

while(i<=$3){

  printf("\t%.2f\n",($1*(1+$2)+i));

  i=i+1;

  }

}

 

수행:

awk -f awk.p1  # 입력화일이 없으므로 standard input

100 0.6 5

 

결과:

        161.00

        162.00

        163.00

        164.00

        165.00

 

위와 동일한 기능의 for:

{

for(i=1;i<=$3;i=i+1)

  printf("\t%.2f\n",($1*(1+$2)+i));

}

 

Array

 

예제:

{ line[NR]=$0}

END{

i=NR

while(i>0){

   print line[i]

   i=i-1

   }

}

 

결과: (역순으로 인쇄)

Susie  4.25 18

Mary   5.50 22

Mark   5.00 20

Kathy  4.00 10

Dan    3.75 0

Beth   4.00 0

 

 

A handful of useful "one-liners"

 

 1. END{print NR}

 2. NR==10

 3. {print $NF}

 4. {field=$NF}

    END{print field}

 5. NF>4

 6. $NF>4

 7. {nf=nf+NF}

    END {print nf}

 8. /Beth/ {nl=nl+1}   #  line에 문자 Beth가 있으면

    END {print ln}

 9. $1>max {max=$1; maxl=$0}

    END{print max, maxl}

10. NF>0

11. length($0)>80

12. {print NF, $0}

13. {temp=$1; $1=$2;$2=temp;print}

15. {$1=NR; print}

16. {$2=""; print}

17. {for(i=NF;i>0;i=i-1) printf("%s ",$i)

     printf("\n") }

18. {sum=0

     for(i=1;i<=NF;i=i+1) sum=sum+$i

     print sum }

19. {for(i=1;i<=NF;i=i+1) sum=sum+$i }

    END{ print sum}

20. {for(i=1;i<=NF;i=i+1)

       if($i<0) $i=-$i

     print }

 

Sample data

 

USSR     8649    275    Asia

Canada    3852    25    North America

China    3705    1032    Asia

USA    3615    237    North America

Brazil    3286    134    South America

India    1267    746    Asia

Mexico    762    78    North America

France    211    55    Europe

Japan    144    120    Asia

Germany    96    61    Europe

England    94    56    Europe

 

 

Patterns

 

formats

 

BEGIN statements

END statements

expression statements

/regular expression/ statements

compound pattern statements

pattern1, pattern2 statements

 

BEGIN and END

예제:

BEGIN{

FS=" "   # FS(field separator) 지정;

         # default blank(space,tab,cr,...)

printf("%10s %6s %5s  %s\n\n",\

       "Country", "Area", "Pop", "Continent")

}

{

printf("%10s %6d %5d  %s\n", $1, $2, $3, $4)

area=area+$2

pop=pop+$3

}

END{

printf("\n%10s %6d %5d\n","Total", area, pop)

}

 

결과:

   Country   Area   Pop  Continent

 

      USSR   8649   275  Asia

    Canada   3852    25  North

     China   3705  1032  Asia

       USA   3615   237  North

    Brazil   3286   134  South

     India   1267   746  Asia

    Mexico    762    78  North

    France    211    55  Europe

     Japan    144   120  Asia

   Germany     96    61  Europe

   England     94    56  Europe

 

     Total  25681  2819

 

 

expression

 

Comparision 

 

Matching ~ (매칭되면), !~ (매칭안되면) /regexpr/, ~/regexpr/, !~/regexpr/

 

 

/Asia/   #  line Asia가 있으면 TRUE

$0 ~ /Asia/   # 바로위와 동일

$4 ~ /Asia/   # $4안에 Asia가 있으면 TRUE

$4 !~/Asia/  # $4안에 Asia가 없으면 TRUE

 

 

/regular expression/ (ref. page28-32)

 

중요:

^ : 첫글짜

$ : 마지막글짜

? : 있어도 되고 없어도 됨

+ : 1번이상 반복

* : 0번이상 반복

 

 

/^[0-9]+$/ : 숫자로만 이루어진 패턴

/^[0-9][0-9][0-9]$/ : 3자리 숫자

/^(\+|-)?[0-9]+\.?[0-9]*$/ : floating point number

/^[a-zA-A][a-zA-Z0-9]*$/ : 변수이름

/^[a-zA-Z][0-9]?$/

 

 

compound pattern

$4=="Asia" && $3 >500

$4=="Asia" || $4 =="Europe"

 

 

pattern1, pattern2

/Europe/, /Africa/   # Europe이 있으면

                     # Africa이 나올때까지(Africa포함)

FNR==1, FNR==5 { print FILENAME ":" $0}   #  file 5line씩만 출력

FNR<=5 {print FILENAME ":" $0}   # 위줄과 동일기능

  # FNR  NR과 유사하나 FNR  file이 바뀌면 reset

 

Expressions

 

Constants

String constant : "Asia", "hello, world", ""

Numeric constant : 1127, 0.707E-1, 1e6 (모든 수는 floating point 형태로 저장)

 

Variables

User-defined : function, built-in variable, field variable도 아닌 경우

Built-In Variables: FNRNF, NR...

Field Variables: $1, $2, ..., $NF, $0

예제:

     BEGIN                 { FS = OFS = "\t" }

       $4 == "North America"    { $4 = "NA"}

      $4 == "South America"    { $4 = "SA" }

                    { print }

 

 

Arithmetic Operators : +, -, *, /, % and ^

 

Comparison Operators : <, <=, ++, !=, >=, >, , !~\

예제:

             $4 ~ /Asia/

 

 

Logical Operators : &&(AND), || (OR), !(NOT)

 

Conditional Expressions : ? :

 

Assignment Operators: var = expr

예제:

        $4 == "Asia"    { pop = pop + $3, n = n + 1 }

    END        { print "Total population of the", n,

                             "Asian countries is", pop, "million." }

 

 

Increment and Decrement Operators : ++, --

 

Built-in Arithmetic Functions : 도표

예제:

     randint = int (n * rand()) + 1

 

 

String Operators : concatenation

예제:

     { print NR ":" $0}

 

 

Strings as Regular Expressions

예제:

 BEGIN {

 sign = "[+-]?"

 decimal = "[0-9]+[.]?[0-9]*"

 fraction = "[.][0-9]+"

 exponent = "([eE]" sign "[0-9]+)?"

 number = "^" sign "(" decimal "|" fraction ")" exponent "&"

        }

      $0 ~ number

 

 

예제:

        $0 ~ /(\+|-)[0-9]+/

        $0 ~ "(\\ +|-)[0-9]+"

 

 

Built-In String Functions

 

예제:

     index("banana", "an")

       returns 2

        Ex 9)

            { $1 = substr($1, 1, 3); print $0 }

 

 

Number or String ? : Automatically converted

예제:

 making string : number ""

      Ex 10)

        $1 "" == $2  

    making number : string + 0

      Ex 11)

 

 

Control-Flow Statements

 

if-else

예제:

 if (expression)

     statement1

 else

     statement

 

 

while

예제:

     {   I = 1

         while (I <= NF) {

             print $I

             I++

         }

     }

 

 

for

 

그외 : do while, break, continue, next, exit

 

Empty Statement

 

예제:

         BEGIN     { FS = "\t" }

            { for (I = 1; I <= NF && $I != ""; I++)

            ;

          if (I <= NF)

            print

        }

 

 

Arrays

 

예제:

         BEGIN    { FS = "\t" }

        { pop[$4] += $3 }

      END    { for ( name in pop )

            print name, pop[name]

        }

 

 

The delete Statement

예제:

     for (I in pop)

        delete pop[I]

 

 

The split Function

예제:

     split( "7/4/76", arr, "/")

 

              arr["1"] <- 7

          arr["2"] <- 4

 arr["3"] <- 76

 

 

Multidimensional Arrays

예제:

     for (I = 1; I <= 10; I++)

         for (j = 1; j <=10; j++)

             arr[I, j] = 0

 

 

User-Defined Functions

 

예제:

  { print max($1, max($2, $3)) }

  function max (m, n) {

        return m > n ? m : n

  }

 

 

Output

 

The Print Statement

Output Separator

예제:

  BEGIN { OFS = ":"; ORS = "\n\n" }

        { print $1, $2 }

 

 

The printf Statement

Output into Files

예제:

  $3 > 100  { print $1, $3 > "bigpop" }

  $3 <= 100 { print $1, $3 > "smallpop" }

 

 

Output into Pipes

예제:

  BEGIN { FS = "\t" }

        {pop[$4] += $3}

  END   { for (c in pop)

        printf("%15s\t%6d\n", c, pop[c]) | "sort"

      }

 

 

Closing Files and Pipes

예제:

    close("sort ")

 

Input

 

Multiline Records

예제:

  BEGIN {RS = ""; FS ="\n" }

 

 

The getline Function

예제:

  /^#include/ {

      gsub( /"/, "", $2)

  while ( getline x <$2> 0)

  print x

  next

 }

 { print }

 

Interaction with Other Programs

Making a Shell Command from an AWK Program

 

예제:

awk '{print $1}' $*

 

 

 

User-Defined Functions

 

# capitalize each word in a string

function capitalize(input,    result, words, n, i, w)

{

             result = ""

             n = split(input, words, " ")

             for (i = 1; i <= n; i++) {

                           w = words[i]

                           w = toupper(substr(w, 1, 1)) substr(w, 2)

                           if (i > 1)

                                        result = result " "

                           result = result w

             }

             return result

}

 

# main program, for testing

{ print capitalize($0) }

With this input data:

 

A test line with words and numbers like 12 on it.

This program produces:

 

A Test Line With Words And Numbers Like 12 On It.

 

 

printf Formats

 

Format specifiers for printf and sprintf have the following form:

 

%[flag][width][.precision]letter

 

The control letter is required. The format conversion control letters are as follows.

 

Character Description

c ASCII character

d Decimal integer

i Decimal integer (added in POSIX)

e Floating-point format ([-]d.precisione[+-]dd)

E Floating-point format ([-]d.precisionE[+-]dd)

f Floating-point format ([-]ddd.precision)

g e or f conversion, whichever is shortest, with trailing zeros removed

G E or f conversion, whichever is shortest, with trailing zeros removed

o Unsigned octal value

s String

x Unsigned hexadecimal number; uses a-f for 10 to 15

X Unsigned hexadecimal number; uses A-F for 10 to 15

% Literal %

 

The optional flag is one of the following.

 

Character Description

- Left-justify the formatted value within the field.

 

space Prefix positive values with a space and negative values with a minus.

 

+ Always prefix numeric values with a sign, even if the value is positive.

 

# Use an alternate form: %o has a preceding 0; %x and %X are prefixed with 0x and 0X, respectively; %e, %E, and %f always have a decimal point in the result; and %g and %G do not have trailing zeros removed.

 

0 Pad output with zeros, not spaces. This happens only when the field width is wider than the converted result.

 

 

The optional width is the minimum number of characters to output. The result will be padded to this size if it is smaller. The 0 flag causes padding with zeros; otherwise, padding is with spaces.

 

The precision is optional. Its meaning varies by control letter, as shown in this table.

 

Conversion Precision Means

%d, %i, %o The minimum number of digits to print

%u, %x, %X

%e, %E, %f The number of digits to the right of the decimal point

%g, %G The maximum number of significant digits

%s The maximum number of characters to print

 

AWK[ awt 사용법 ]

 

 

 - 화면출력 '{ print }'

 

 - 입력된 행을 자동으로 공백을 기준하여 필드 단위로 나눈다앞 필드부터 $1 $2 $3 ...

 

   ) route | grep UH | grep eth1 | awk '{ print $1 }' | grep -v "255.255.255.255"

   결과) IP만 뽑아 온다.

          192.168.1.33

          192.168.1.132

          192.168.1.3

 

 - -F: 구분문자(:)를 지정하여 나눌 수 있다.

   ) head /etc/passwd | awk -F: '{ print $1, $7 }'

   결과) :를 구분문자로 나누어 첫번째일곱번째 인자를 뽑아온다.

          root /bin/csh

          nobody

          daemon

 

각종 예제 -

 

# DHCP 동적IP할당된 IP를 얻어오기

$ /sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | sed -e 's/.*://'

$ /sbin/ifconfig eth0 | /bin/grep 'addr:' | /bin/awk '{ print $2 }' | /bin/awk -F: '{ print $2 }'

  결과: 210.219.177.223

 

임시 .check_ip_allow.diff 파일에서 다른 부분(허가 되지 않은 인터넷 접속자)은 인터넷을 차단한다.

l=`/bin/grep "<" /tmp/.check_ip_allow.diff | /usr/bin/awk '{ print $2 }'`

for a in $l; do

             ipchains -D input -s $a -j ACCEPT

done

 

임시 .check_ip_allow.diff 파일에서 같은 부분(허가 되지 않은 인터넷 접속자)은 인터넷을 허가 한다. - 반복됨이 문제

l=`/bin/grep ">" /tmp/.check_ip_allow.diff | /usr/bin/awk '{ print $2 }'`

for a in $l; do

             ipchains -I input -s $a -j ACCEPT

done

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

awk 한 라인으로 할수 있는 여러가지 일들!

 

원본출처: 'The AWK Programming Language'

원저자: Alfred V. Aho

             Brian W. Kernighan

             Peter J. Weinberger

편집안창선(csan@coresw.co.kr, http://genesis.yonsei.ac.kr/~kabin)

 

1. 텍스트 파일의 전체 라인수를 계산

 

 END { print NR }

 

2. 텍스트 파일의 특정번째 라인의 문자열을 프린트

 

 NR == 10

 

3. 각 라인의 맨끝 단어를 프린트

 

 { print $NF }

 

4. 마지막 입력 라인의 마지막 필드를 프린트

 

 { field = $NF }

END { print field }

 

5. 4개 이상의 필드(단어)를 가지고 있는 라인을 프린트

 

 NF > 4

 

6. 마지막 필드의 값이 4 이상인 라인을 프린트

 

 $NF > 4

 

7. 모든 입력 라인에서의 총 필드의 갯수를 구함

 

 { nf = nf + NF }

 END { print nf }

 

8. 'fly'를 포함하고 있는 라인의 총수 계산

 

 /fly/ { nlines = nlines + 1 }

 END { print nlines }

 

9. 가장 긴 첫번째 필드(단어)와 그 해당 라인을 출력

 

 $1 > max { max = $1; maxline = $0 }

 END       { print max, maxline }

 

10. 적어도 한 필드(단어)를 포함하고 있는 모든 라인을 프린트

 

 NF > 0

 

11. 80문자 이상을 가진 모든 라인을 프린트

 

 length($0) > 80

 

12. 필드의 갯수와 해당 라인을 프린트

 

 { print NF, $0 }

 

13. 첫번째 두 필드를 반대로 프린트

 

 { print $2, $1 }

 

14. 첫번째 두 필드를 반대로 바꾼 다음그 라인을 프린트

 

 { temp = $1; $1 = $2; $2 = temp; print }

 

15. 첫번째 필드는 라인 번호로 대체하여 그 라인을 프린트

 

 { $1 = NR; print }

 

16. 두번째 필드는 삭제한 후 라인을 프린트

 

 { $2=""; print }

 

17. 각 라인의 모든 필드를 역순으로 프린트

 

 { for (i=NF; i> 0; i = i-1) printf("%s ", $i)

             printf("\n")

 }

 

18. 각각의 라인에 대하여 각 필드를 더한 값을 프린트

 

 { sum=0

   for (i=1;i<=NF;i=i+1) sum=sum+$i

   print sum

 }

 

19. 모든 라인에 존재하는 필드를 모두 더한 값을 프린트

 

 { for (i=1;i<=NF;i=i+1) sum=sum+$i }

 END { print sum }

 

20. 모든 라인에 대하여 각 필드의 절대값을 그 필드로 교체하여 그 라인을 프린트

 

 { for (i=1;i<=NF;i=i+1) if($i < 0) $i=-$i

   print

 }

 

 

AWK system variable

 

awk       FILENAME           현재파일명

             FS                       필드 구분자

             NF                      현재라인의 필드 수

             NR                      현재라인의 레코드 번호

             OFMT                 숫자 출력을 위한 포맷(예를 들어  %.6g)

             OFS                    출력 필드 구분자(디폴트는 blank)

             ORS                    출력 레코드 구분자(디폴트는 newline)

             RS                       레코드 구분자(디폴트는 newline)

             $0                       전체 입력라인

             $n                       n번째 필드의 내용(각 필드는 FS로 구분된다.)

 

nawk     ARGC                  명령라인의 아규먼트 수

             ARGV                  명령라인의 배열

             ENVIRON                          환경변수배열

             FNR                    NR과 같다그러나 현재 파일과 관련이 있다.

             RSTART              match함수에 의해 매치된 문자열에서의 첫번째 위치

             RLENGTH                         match함수에 의해 매치된 문자열의 길이

             SUBSEP              배열 서브스크립트를 위한 문자구분자(디폴트는 \034)

 

AWK 명령들

 

수치연산

 

atan2     atan2(y,x)

cos       cos(x)

exp        exp(arg)

int          int(arg)

log         log(arg)

rand       rand()

sin         sin(x)

sqrt       sqrt(arg)

srand     srand(expr)

 

문자열연산

 

gsub      gsub(r,s[,t])

index     index(str,substr)

length    length(arg)

match    match(s,r)

split       split(string,array[,sep])

sub        sub(r,s[,t])

substr   substr(string,m[,n])

tolower  tolower(str)

toupper toupper(str)

 

제어문

 

break

continue

do/while

exit

for

if

return

while

 

입출력/프로세스

 

close     close(filename-expr)

delete    delete array[element]

getline   getline [var][<file]

next       next

print       print [args][destination]

printf      printf format [, expression(s)][destination]

sprintf    sprintf (format [,expression(s)])

system  system(command)

 

[출처] AWK 기술 자료|작성자 몽크


by 버려진Eye 2013. 3. 15. 17:00