AWK 기초

Network/Ns-2 | 2006/12/19 19:47 | adioshun
AWK
  1. awk는 직접 사용자로부터 입력을 받거나 아니면 지정한 파일을 가공하여 표준 출력한다
    표준 출력을 리다이렉션할 수 있다 
  2. 사용법
    • awk   [옵션]   '스크립트'   [-v 변수=값]   [파일(들)]
    • awk   [옵션]   -f  스크립트 파일   [-v 변수=값]   [파일(들)]
    • cf) 편집 스크립트 파일의 사용법
      • ed  : ed -s(script) sourcefile < scriptfile
      • sed :  sed -f(file) scriptfile sourcefile > outputfile
      • awk : awk -f(file) scriptfile sourcefile > outputfile
  3. 옵션
    • -Fc  :  field separator 지정
      • c는 필드 사이를 구분하는 구분자이다
      • 직접 지정하지 않으면 공백을 기준으로 한다
      • 시스템 변수 FS를 지정하는 것과 같은 효과를 지닌다
    • -v  변수 = 값
      • 스크립트를 실행하기 전에 미리 변수를 지정하여 준다
    • -f  스크립트 파일
      • 스크립트를 파일에서 가져온다  
      • -f 옵션을 여러번 사용하여 여러개의 스크립트 파일을 동시에 불러와 지정한 파일에 적용할 수 있다 
  4. 스크립트
    • 패턴 { 동작 }
      커맨드 라인에서는 패턴, 동작 전체를 단일 따옴표로 묶는다
      • 패턴만 있는 경우 : 패턴과 일치하는 레코드(라인)를 화면에 출력한다 
      • 동작만 있는 경우 : 모든 레코드(라인)가 동작의 대상이 된다
    • 패턴
      1. /정규표현식/
        sed가 지원하지 않는 +, ?, |, ( ) 등의 메타문자도 지원한다   또한
        ^, $를 각 필드의 처음과 끝을 의미하도록 사용할 수도 있다
      2. 비교연산
        숫자 기준, 알파벳 기준 모두 사용 가능하다 
      3. 패턴 매칭 연산
        ~  :  일치하는 부분을 나타낸다
        !~ :  일치하지 않는 부분을 나타낸다
      4. BEGIN
        첫 번째 레코드가 읽혀지기 전에 어떤 동작을 정의하여 사용하고 싶을 때 사용한다 
      5. END
        마지막 레코드가 모두 읽혀진 후 어떤 동작을 정의하여 실행하고 싶을 때 사용한다 
    • 동작
      • 동작은 모두 { }로 둘러싸야 한다
      • 예제
        • good이라는 문자열을 포함하는 모든 레코드를 출력할 때
          /good/
        • 각 레코드의 첫 번째 필드를 출력할 때
          { print $1 } 
        • good이라는 문자열을 포함하는 레코드의 첫 번째 필드를 출력할 때
          /good/ { print $1 } 
        • 두 개 이상의 필드를 가지는 레코드를 전부 출력할 때(비교연산)
          NF > 2
        • 한 라인(n)을 필드로, 빈 라인("")을 레코드로 구분할 때
          BEGIN { FS = "n" ;  RS = ""} 
        • 첫 번째 필드가 good와 일치하는 레코드에 대해 세 번째 필드를 먼저 출력하고 두 번째 필드를 나중에 출력하고 싶을 때
          $1 ~ /good/ { print  $3 ,  $2 }
        • good이라는 문자열이 몇 개나 들어가 있는지 계산하여 마지막 부분에서 출력하고 싶을 때
          /good/ { ++x }
          END { print x } 
        • 두 번째 필드를 모두 합하고 마지막 부분에서 두 번째 필드의 총합계를 출력하고 싶을 때
          { total += $2 }
          END { print "Total of $2: " ,  total } 
        • 레코드의 길이가 20자 이하인 것을 출력하고 싶을 때
          length($0) < 20 
        • 네 개의 필드를 가지며 good이라는 단어로 시작하는 모든 레코드를 출력하고 싶을 때
          NF == 4  &&  /^good/
        • 빈줄을 제외한 모든 줄을 화면에 출력한다
          NF > 0
  5. awk 시스템 변수
      FILENAME
      현재 파일명
      $0
      입력 레코드
      FS
      입력 필드 구분
      디폴트 :  공백
      $n
      입력 레코드의 N번째 필드
      NF
      현재 레코드 필드 갯수
      ARGC
      커맨드 라인의 인자 갯수
      NR
      현재 레코드 번호
      ARGV
      커맨드 라인 인자를 포함하는 배열
      OFMT
      숫자에 대한 출력 포맷
      디폴트 :  %.6g
      ENVIRON
      환경 변수들을 모아둔 관계형 배열
      OFS
      출력 필드 구분
      디폴트 :  빈줄
      FNR
      NR과 동일
      단지 현재 파일에 적용된다는 점이 다름
      ORS
      출력 레코드 구분
      디폴트 :  newline
      RSTART
      지정한 매칭 연산을 만족하는 문자열의 맨 앞부분
      RS
      입력 레코드 구분
      디폴트 :  newline
      RLENGTH
      지정한 매칭 연산을 만족하는 문자열의 길이
  6. awk 연산자
      산술 : =, +=, -=, *=, /=, %=
      조건 : ? :
      논리 : ||, &&, !
      패턴 : ~, !~
      비교 : <, <=, >, >=, !=,==
      증감 : ++, --
      필드참조 : $
  7. 제어문(C의 제어문과 같다)
    • break
    • continue
    • do {실행} while (조건)
    • exit
    • for (관계형 배열의 요소) {실행}
      펄의 foreach와 같다
    • if (조건) {실행} else {실행}
    • return
    • while
  8. awk 명령어
    • 문자열 연산
      • gsub(reg,s)
        입력 문자열의 전반에 걸쳐 정규표현식 r을 문자열 s로 대치한다
      • gsub(reg,s1,s2)
        문자열 s2에서 정규표현식 r을 s1으로 대치한다 
      • index(s1,s2)
        s1에서 s2의 위치를 넘겨준다  만약 없다면 0을 넘겨준다 
      • length(arg)
        인자의 길이를 넘겨준다 
      • match(s,r)
        문자열 s에서 정규표현식 r과 매칭되는 부분의 위치를 넘겨준다 
      • split(string,array[,seperator])
        구분자를 기준으로(지정하지 않으면 공백 기준)해서 지정한 문자열을 배열로 만든다  배열[1],  배열[2], ....... 
      • sub(r,s),  sub(r,s1,s2)
        gsub과 동일하다
        단지 정규표현식과 일치하는 문자열이 여러개라도 처음 문자열만 대치된다
      • substr(s,m)
        문자열 s에서 m번째 위치에서 끝까지의 문자를 리턴한다 
      • substr(s,m,n)
        문자열 s에서 m번째 위치에서 n번째까지의 문자를 리턴한다 
      • tolower(str)
      • toupper(str)
    • 수치 연산
      • atan2(x,y)
        y/x의 arctangent값을 라디안 단위로 넘겨준다 
      • cos(x)
      • exp(arg)
      • int(arg)
      • log(arg)
      • rand()
        0과 1사이의 난수를 발생한다 
      • sin(x)
      • sqrt(arg)
      • srand(expr)
        인자를 가지고 난수를 발생한다
        인자가 주어지지 않으면 시간을 가지고 난수를 발생한다 
    • 입출력/프로세스
      • close(filename)
        지정한 파일을 닫는다 
      • close(cmd)
        지정한 명령어 파이프를 닫는다 
      • delete array[element]
        지정한 배열 요소를 지운다 
      • getline()
        다음 레코드를 읽어 들인다 
      • getline[variable] [< "filename"]
        파일에서 읽어들인다 
      • next
        다음 레코드(라인)을 입력받는다
        getline()과 유사하지만 /패턴/동작을 새롭게 시작한다
        getline()은 다음 라인을 읽기만 한다 
      • print [args] [> "filename"]
        인자를 출력한다 
      • printf "format" [,expressions] [> "filename"]
        형식에 맞춰 출력한다 
      • sprintf (format [,expressions])
        printf와 마찬가지로 사용하는데 값을 리턴하기만 하고 출력은 하지 않는다 
      • system(command)
        시스템 내부 명령어를 실행한다 
  9. 간단한 예
    • awk  ' BEGIN { for (i = 1;i<=7,i++)  print int(101*rand()) }'
      화면에 1이상 100이하의 난수 일곱 개를 출력한다
    • ls -l  file1  file2  file3  | awk  ' { x += $5 } ;  END { print "Total bytes :  " x } '
      파일들의 크기를 모두 합하여 총 바이트 수를 표시한다
    • awk  ' END { print NR } ' filename
      지정한 파일의 라인이 몇 개인지를 표시한다
    • awk  ' NR % 2 == 0 '
      지정한 파일의 짝수번째의 라인만을 출력해 준다 
2006/12/19 19:47 2006/12/19 19:47
TAG ,
Trackback address :: http://4ellene.net/tt/trackback/1087
  1. AWK 기초

    Tracked from 볼거없는블로그, 2006/12/26 20:22  삭제

    <FONT style ='FONT-SIZE: 9pt; FONT-FAMILY: 1144591_9;'><TABLE style="TABLE-LAYOUT: fixed" cellSpacing=0 cellPadding=0 border=0> <TBODY> <TR> <TD> <DIV snap_preview_checked="true"><FONT color=#400000 size=4><B>AWK</B></FONT> <OL> <LI>awk는 직접..

  2. Soma.

    Tracked from Pornstar soma. 2008/07/06 18:57  삭제

    Buy soma online.

  3. full tilt poker

    Tracked from poker stars 2009/03/30 04:57  삭제

    poker chips

  4. Tracked from cruel rape 2009/04/13 06:08  삭제

    crime rape

  5. Tracked from sickest rape sites 2009/04/13 16:19  삭제

    sexy rape

  6. Tracked from daughter incest rape stories 2009/04/13 20:15  삭제

    date rape porn

  7. forced sex porn

    Tracked from forced sex reporter 2009/04/14 15:33  삭제

    forced sex pictures

  8. rape scenes in movies

    Tracked from rape section 2009/04/14 16:03  삭제

    rape scenes

  9. black bdsm

    Tracked from ebony bdsm 2009/04/15 01:49  삭제

    bdsm vids

  10. bdsm free videos

    Tracked from bdsm galleries 2009/04/15 11:02  삭제

    bdsm free

  11. black rape porn

    Tracked from drug rape girl porn 2009/06/29 22:24  삭제

    violent porn

  12. sister rape

    Tracked from forced dog sex 2009/06/30 01:29  삭제

    rough forced sex movies

  13. women rape

    Tracked from forced sex gallery 2009/06/30 02:46  삭제

    forced fantasies

  14. rape incest

    Tracked from rape club 2009/06/30 12:18  삭제

    rape asain

  15. sister rape

    Tracked from young rape victims 2009/06/30 22:33  삭제

    how to rape

  16. fantasy rape stories

    Tracked from forced to have sex 2009/07/01 05:08  삭제

    rape fantasy

  17. lesbian forced sex

    Tracked from father rape 2009/07/01 05:12  삭제

    rape tales

  18. photos of drunk rape women

    Tracked from brother rapes sister 2009/07/01 06:45  삭제

    rape films

  19. rape porn tube

    Tracked from violent fucking 2009/07/01 11:38  삭제

    rape porn video

  20. lesbian forced sex

    Tracked from rape prevention 2009/07/01 14:25  삭제

    toon rape

  21. forced rape

    Tracked from forced blowjobs 2009/07/01 22:30  삭제

    underground rape porn

  22. rape scene

    Tracked from forced fantasy 2009/07/02 05:33  삭제

    be forced

  23. hot rape

    Tracked from rape sites 2009/07/02 06:06  삭제

    teen rape

  24. sexy rape

    Tracked from forced sex porn 2009/07/02 08:21  삭제

    rape galleries

  25. stories about rape

    Tracked from brutal hentai 2009/07/02 16:39  삭제

    gang rape videos

  26. rape porn

    Tracked from forced sex galleries 2009/07/02 21:14  삭제

    sickest rape sites

  27. oral rape

    Tracked from rape club 2009/07/03 00:00  삭제

    hentai rape

  28. forced bi

    Tracked from rape and abortion 2009/07/04 13:49  삭제

    sexual assault rape

  29. online casinos accepting us

    Tracked from free casino slots 2009/07/10 03:54  삭제

    online casino reviews

  30. online casino us players

    Tracked from casino sites 2009/07/10 22:07  삭제

    beating internet casino

  31. online no deposit casinos

    Tracked from online usa casinos 2009/07/11 06:34  삭제

    web casino

  32. online casino craps

    Tracked from casino http 2009/07/11 11:33  삭제

    online casinos no deposit

  33. casino websites

    Tracked from online no deposit casinos 2009/07/11 13:19  삭제

    online casino australia

  34. golden casino http

    Tracked from casino party usa 2009/07/12 07:57  삭제

    highest payouts online casino

  35. online betting casinos

    Tracked from free cash online casino 2009/07/12 08:19  삭제

    free casino online

  36. golden casino http

    Tracked from online casino s 2009/07/12 19:19  삭제

    no download online casinos

  37. casino online apuestas

    Tracked from no deposit online usa casion 2009/07/13 11:18  삭제

    online casino http

  38. no deposit online casino listings usa

    Tracked from casinos http 2009/07/14 16:02  삭제

    online casinos

  39. usa internet casino

    Tracked from online casino no depost 2009/07/15 00:13  삭제

    online casinos accepting all usa players

  40. live online poker

    Tracked from online casino deposit 2009/07/15 04:44  삭제

    free online casino games

  41. free casino slots

    Tracked from free online casinos 2009/07/15 11:10  삭제

    no download casino

  42. internet casino

    Tracked from online casino games http 2009/07/16 04:09  삭제

    casino website

  43. play casino games online

    Tracked from no deposit online casinos 2009/07/16 04:50  삭제

    online internet casino

  44. casino betting

    Tracked from casinos 2009/07/16 13:00  삭제

    no bonus online casinos

  45. directorio casino online

    Tracked from online casino australia 2009/07/16 18:15  삭제

    top online casinos

  46. top online casinos

    Tracked from online casino games 2009/07/17 06:36  삭제

    usa online casino

  47. list of online casinos

    Tracked from online casino register 2009/07/17 20:25  삭제

    online casino s

  48. online casino

    Tracked from vip casin 2009/07/17 22:34  삭제

    u s online casinos

  49. reputable online casino

    Tracked from usa internet casino 2009/07/18 04:15  삭제

    online casino blackjack

  50. the online casino

    Tracked from usa internet casino 2009/07/18 07:40  삭제

    play free casino games

  51. usa online casino

    Tracked from internet casino gambling 2009/07/19 15:01  삭제

    play casino games online

  52. winning online poker

    Tracked from casino 2009/07/20 01:30  삭제

    topgame online casinos

  53. post op transsexuals

    Tracked from transvestite bdsm 2009/07/20 06:56  삭제

    post op transsexuals

  54. singapore transexuals

    Tracked from transexual escort london 2009/07/20 07:18  삭제

    japanese ladyboy

  55. futanari links

    Tracked from sexy shemale 2009/07/20 13:46  삭제

    las vegas transvestite

  56. ladyboys fucking

    Tracked from shemale bondage 2009/07/20 20:23  삭제

    transvestite cock

  57. futanari fiction

    Tracked from shemale clips 2009/07/20 23:57  삭제

    shemale on shemale

  58. shemale fucking girl

    Tracked from transexual vanilla 2009/07/21 08:22  삭제

    amateur transvestite

  59. tranny pics

    Tracked from transgenders 2009/07/21 13:08  삭제

    ladyboys galleries

  60. ladyboys in brazil

    Tracked from submissive transsexual 2009/07/22 06:36  삭제

    ladyboy tube

  61. transsexual sex

    Tracked from tranny fuck 2009/07/22 22:44  삭제

    ladyboy stories

  62. transexual chat

    Tracked from futanari manga 2009/07/23 03:42  삭제

    tranny on tranny

  63. futanari manga

    Tracked from futanari hentai 2009/07/23 13:05  삭제

    chompoo ladyboy

  64. shemale list

    Tracked from futanari gallery 2009/07/23 19:14  삭제

    beautiful transexuals

  65. ladyboy 69

    Tracked from transvestite sissy maids 2009/07/23 22:11  삭제

    tranny orgy

  66. shemale cock

    Tracked from transexual pubic hair designs 2009/07/24 04:24  삭제

    black transexual

  67. black ladyboy

    Tracked from futanari doujinshi 2009/07/24 05:25  삭제

    black transvestite

  68. shemale yum

    Tracked from tranny creampie 2009/07/25 04:13  삭제

    transexual

  69. dad and son gay sex

    Tracked from dad and daughter incest 2009/09/06 00:01  삭제

    dad fucking daughter

  70. daughter sex

    Tracked from father daughter sex 2009/09/06 04:29  삭제

    daddies fucking daughters

  71. incest pics

    Tracked from free incest 2009/09/06 10:16  삭제

    preteen incest

  72. Mom Mature Son Gallery

    Tracked from Free Mom And Son Sex 2009/09/06 13:37  삭제

    Mom Strips for Son

  73. mother daughter incest

    Tracked from mother daughter sex 2009/09/07 00:36  삭제

    lesbian mother daughters

  74. young incest

    Tracked from twin sisters naked 2009/09/07 00:38  삭제

    brother sister sex

  75. family sex

    Tracked from young incest 2009/09/07 04:52  삭제

    family incest

  76. sister fucking brother

    Tracked from sister nude 2009/09/08 05:25  삭제

    brother on sister porn

  77. brother and sister sex stories

    Tracked from little sister 2009/09/08 07:19  삭제

    brother sister incest

  78. family nudist galleries

    Tracked from family nude 2009/09/08 19:38  삭제

    family orgy

  79. nude sister

    Tracked from nude family 2009/09/08 23:46  삭제

    simpsons family sex

  80. hentai incest

    Tracked from free cartoon incest 2009/09/09 12:20  삭제

    dad fucking daughter

  81. incest rape

    Tracked from incest pictures 2009/09/09 12:27  삭제

    incest taboo

  82. sister fucking brother

    Tracked from simpsons family sex 2009/09/09 23:46  삭제

    sister incest

  83. incest taboo forum

    Tracked from incest taboo 2009/09/10 01:31  삭제

    incest toons

  84. dick torture

    Tracked from diaper bondage 2009/09/11 05:31  삭제

    domination

  85. bdsm story

    Tracked from bdsm stories 2009/09/11 22:34  삭제

    bdsm teen

  86. bdsm torture

    Tracked from bdsm toons 2009/09/12 02:12  삭제

    bdsm toys

  87. slave torture stories

    Tracked from slave girl 2009/09/12 02:57  삭제

    slave training

  88. bdsm sex movies

    Tracked from bdsm sex 2009/09/12 07:12  삭제

    bdsm sites

  89. extreme bdsm

    Tracked from enema torture 2009/09/12 10:13  삭제

    extreme bondage

  90. latex bdsm

    Tracked from hogtied bondage 2009/09/12 10:54  삭제

    lesbian bdsm

  91. female domination

    Tracked from female bondage 2009/09/12 20:22  삭제

    female torture

  92. bdsm movies

    Tracked from bdsm humiliation 2009/09/13 01:40  삭제

    bdsm mpegs

  93. bdsm porn

    Tracked from bdsm playpen 2009/09/13 02:11  삭제

    bdsm sex movies

  94. bondage movie

    Tracked from bondage girls 2009/09/13 04:29  삭제

    bondage movies

  95. girl bondage

    Tracked from genital torture 2009/09/14 09:58  삭제

    groin torture

  96. bondage

    Tracked from bedroom bondage 2009/09/14 13:14  삭제

    bondage anal

  97. bdsm tgp

    Tracked from bdsm teen 2009/09/14 13:59  삭제

    bdsm thumbs

  98. electric torture

    Tracked from dungeon torture 2009/09/15 10:09  삭제

    enema torture

  99. female domination

    Tracked from female bondage 2009/09/15 20:55  삭제

    female domination stories

  100. bondage orgasm

    Tracked from bondage movies 2009/09/16 06:47  삭제

    bondage paper

  101. amateur bdsm free videos

    Tracked from amateur bdsm 2009/09/16 09:27  삭제

    anal bdsm

  102. nipple torture

    Tracked from needle torture 2009/09/16 11:30  삭제

    outdoor bdsm

  103. bdsm videos

    Tracked from bdsm video 2009/09/16 11:55  삭제

    bdsm vids

  104. father fucks daughter

    Tracked from dad fucking virgin daughter 2009/09/27 11:24  삭제

    fathers fucking daughters

  105. fathers fucking daughters

    Tracked from dad fucking virgin daughter 2009/09/27 12:25  삭제

    father fucks daughter

  106. dad daughter nude sex

    Tracked from dad and daughter sex 2009/09/27 14:15  삭제

    daughter incest incest

  107. daddies and sons gay

    Tracked from family rape 2009/09/28 21:18  삭제

    gay incest

  108. incest videos

    Tracked from incest pictures 2009/09/29 10:37  삭제

    incest pics

  109. little sister

    Tracked from fuck my sister wet wet pussy 2009/09/29 13:46  삭제

    brother and sister sex stories

  110. father daughter sex

    Tracked from dad fucking daughter 2009/10/01 18:58  삭제

    dad fucks daughter

  111. fathers fucking daughters

    Tracked from father son sex 2009/10/01 19:31  삭제

    free incest pics

  112. nude family

    Tracked from family incest 2009/10/02 10:50  삭제

    Family Porn

  113. nude family

    Tracked from my sisters hot friend 2009/10/03 08:33  삭제

    nude sister

  114. Fucked Her Son

    Tracked from incest lesbian stories 2009/10/03 10:56  삭제

    Moms Fucking Sons

  115. little sister

    Tracked from fuck my sister wet wet pussy 2009/10/04 06:41  삭제

    brother and sister sex stories

  116. bdsm 666

    Tracked from bdsm anal 2009/10/20 09:22  삭제

    bdsm art

  117. bdsm 666

    Tracked from bdsm anal 2009/10/20 15:02  삭제

    bdsm art

  118. bdsm ebooks

    Tracked from bdsm fiction 2009/10/22 12:13  삭제

    bdsm for all

  119. bdsm free

    Tracked from bdsm free videos 2009/10/22 14:35  삭제

    bdsm galleries

  120. bondage girl

    Tracked from bondage girls 2009/10/22 16:03  삭제

    bondage hentai

  121. bdsm clips

    Tracked from bdsm comics 2009/10/23 16:33  삭제

    bdsm comix

Comments List

  1. ju 2007/02/02 15:08

    좋은 자료 감사 합니다. 퍼갈께요!!

  2. swguru 2007/09/21 06:49

    유용한 awk 코드를 올리셨네요.
    http://swguru.kr/ 에서도 올려주시면 님에게도 도움이 될 것 같습니다 ^^

  3. man and female dog 2008/05/23 04:50

  4. nyny ladies 2008/05/23 05:07

  5. gay latin indexof 2008/05/23 06:02

  6. close up nudes 2008/05/23 07:09

  7. chubby fat chicks 2008/05/24 00:53

Write a comment.

[로그인][오픈아이디란?]