grep、sed、awk

5468 단어
grep, sed, awk 용법 을 지속 적 으로 정리 합 니 다.
아주 중요 한 정규 표현 식 을 맨 앞 에 쓰 세 요.
^
        :/^sed/     sed    。
$
        :/sed$/     sed    。
.
             :/s.d/  s        ,   d。
*
          :/*sed/                 sed  。
[]
            , /[Ss]ed/  sed Sed。
[^]
              , :/[^A-RT-Z]ed/     A-R T-Z       ,  ed  。
\(..\)
       , s/\(love\)able/\1rs,loveable    lovers。
&
              , s/love/**&**/,love  **love**。
\<
       , :/\<love/     love       。
\>
       , /love\>/     love       。
x\{m\}
    x,m , :/0\{5\}/    5 o  。
x\{m,\}
    x,  m , :/o\{5,\}/     5 o  。
x\{m,n\}
    x,  m ,   n , :/o\{5,10\}/  5--10 o  。

grep
자 리 를 비 워 두 고 기다리다.
sed
  :d  
$ sed '2d' example-----  example      。

$ sed '2,$d' example-----  example            。

$ sed '$d' example-----  example       。

$ sed '/test/'d example-----  example      test  。

  :s  
$ sed 's/test/mytest/g' example-----       test   mytest。    g  ,           test    mytest。

$ sed -n 's/^test/mytest/p' example-----(-n)   p                   。    ,        test    mytest,    。

$ sed 's/^192.168.0.1/&localhost/' example-----&                 。   192.168.0.1              localhost,  192.168.0.1localhost。

$ sed -n 's/\(love\)able/\1rs/p' example-----love    1,  loveable     lovers,            。

$ sed 's#10#100#g' example-----      ,   s             ,  ,“#”       ,      “/”   。     10   100。

      :  
$ sed -n '/test/,/check/p' example-----     test check             。

$ sed -n '5,/^test/p' example-----               test          。

$ sed '/test/,/check/s/$/sed test/' example-----    test west    ,         sed test  。

    :e  
$ sed -e '1,5d' -e 's/test/check/' example-----(-e)               。     ,       1 5 ,      check  test。             。            ,                      。

$ sed --expression='s/test/check/' --expression='/love/d' example-----   -e      --expression。   sed     。

     :r  
$ sed '/test/r file' example-----file        ,    test      ,      , file               。

    :w  
$ sed -n '/test/w file' example----- example     test      file 。

    :a  
$ sed '/^test/a\\--->this is a example' example<-----'this is a example'     test      ,sed    a        。

  :i  
$ sed '/test/i\\

new line

-------------------------' example

  test   ,                   。

   :n  
$ sed '/test/{ n; s/aa/bb/; }' example-----  test   ,           ,      aa,  bb,     ,    。

  :y  
$ sed '1,10y/abcde/ABCDE/' example----- 1--10    abcde     ,  ,                。

  :q  
$ sed '10q' example-----    10  ,  sed。

     :h   G  
$ sed -e '/test/h' -e '$G example----- sed       ,                      ,             ,                 。         ,           。      ,  test      ,       ,h                         。         ,        ,G           ,           ,                     。               。    ,    test                。

     :h   x  
$ sed -e '/test/h' -e '/check/x' example -----               。      test check    。
a   i  
a    append, i    insert,         。 :

#    1i  ,    1      (insert)
$ sed "1 i This is my monkey, my monkey's name is wukong" my.txt
This is my monkey, my monkey's name is wukong
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
 
#    1a  ,            (append)
$ sed "$ a This is my monkey, my monkey's name is wukong" my.txt
This is my cat, my cat's name is betty
This is my monkey, my monkey's name is wukong
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam

$ sed "/fish/a This is my monkey, my monkey's name is wukong" my.txt

c  
c         
$ sed "2 c This is my monkey, my monkey's name is wukong" my.txt
This is my cat, my cat's name is betty
This is my monkey, my monkey's name is wukong
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
 
$ sed "/fish/c This is my monkey, my monkey's name is wukong" my.txt
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my monkey, my monkey's name is wukong
This is my goat, my goat's name is adam
#    1i  ,    1      (insert)
$ sed "1 i This is my monkey, my monkey's name is wukong" my.txt
This is my monkey, my monkey's name is wukong
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
 
#    1a  ,            (append)
$ sed "$ a This is my monkey, my monkey's name is wukong" my.txt
This is my cat, my cat's name is betty
This is my monkey, my monkey's name is wukong
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam

줄 머리 에 작은 따옴표 추가
sed "s/^/\"&/g" test 

여러 명령 실행
sed '/./{s/^/HEAD&/;s/$/&TAIL/}' test.file

좋은 웹페이지 즐겨찾기