정규 표현 식 과 사 총사
                                            
 9376 단어  정칙표현 식사 총사Linux 지식 포인트
                    
     :
        ,              ,         、                 。          :
1、        ,    
2、    
3、                             :        、  、   ,        grep、egrep、sed、awk
   :           ,       。find         :
       :BRE,        
       :ERE,                 linux      :
vi/vim   
grep(         )
egrep(        )
sed(        )
awk(                 )          :
  :               ,                           。
   :
?:        0   1   
*:  0      ,   ......  
find / -name “a?”
find / -name “a*”  ^[0-9]+abc$
 :
^:     
[0-9]:    ,      
+:    ,      
abc$: abc  
^[a-z0-9_-]{3,15}$
 :
^[a-z0-9_-]:^       ;a-z    a-z;0-9    0-9;   ;   
{3,15}:3~15     
$:                   :  문자
역할.
\
전의 문 자 는 특수 기호의 의 미 를 취소 하 는 데 사 용 됩 니 다. 예 를 들 어:!,기다리다
^
문자열 의 시작 위치 와 일치 합 니 다. 예 를 들 어: ^ World 는 World 로 시작 하 는 줄 과 일치 합 니 다.
$
월 드 $가 월 드 로 시작 하 는 줄 과 일치 하 는 문자열 의 끝 위치 입 니 다.
.
줄 바 꿈 을 제외 한 임의의 문자 와 일치 합 니 다.
*
앞의 하위 표현 식 과 0 번 또는 여러 번 일치 합 니 다.
[list]
list 목록 의 문자 와 일치 합 니 다. 예 를 들 어 [0 - 9] 는 모든 숫자 와 일치 합 니 다.
[^list]
list 목록 에 없 는 문자 와 일치 합 니 다. 예 를 들 어 [^ 0 - 9] 는 비 숫자 문자 와 일치 합 니 다.
{n}
앞의 하위 표현 식 n 회 와 일치 합 니 다. 예 를 들 어 [0 - 9] {2} 은 두 자리 숫자 와 일치 합 니 다.
{n,}
앞의 하위 표현 식 과 일치 하 는 것 은 n 번 보다 적지 않 습 니 다. 예 를 들 어 [0 - 9] {2,} 은 두 자리 와 두 자리 이상 의 숫자 를 표시 합 니 다.
{n,m}
앞의 하위 표현 식 n 에서 m 까지 일치 합 니 다. 예 를 들 어 {a - z} {2, 3} 에서 두 세 자리 까지 일치 하 는 소문 자 입 니 다.
           :  문자
역할.
+
앞의 표현 식 과 1 회 이상 일치 합 니 다. 예 를 들 어 go + d 는 최소한 o 와 일치 합 니 다.
?
앞의 하위 표현 식 과 0 번 또는 1 번 일치 합 니 다. 예 를 들 어 go? d 는 gd 나 god 와 일치 합 니 다.
()
() 번호 의 문자열 을 정수 로 합 니 다. 예 를 들 어 (xzy) + 는 xyz 전체 와 1 회 이상 일치 합 니 다.
파이프 기호
good 파이프 기호 great 와 같은 문자열 을 일치 시 키 거나 일치 시 킵 니 다. good 또는 great 와 일치 합 니 다.
   :grep、egrep、sed、awk  grep  
-i:     
-v:  
   
   
grep ‘root’ /etc/passwd       //       root  
grep -v ‘root’ /etc/passwd      //        root  
 :-v       ,     
grep ‘r..d’ /etc/passwd       //  r d         
grep ‘[^s]bin’ /etc/passwd     //  bin    s  
grep ‘^$’ /etc/passwd       //      
grep ‘t[es]’ /etc/passwd      //       te ts  
grep ‘0\{1,\}’ /etc/passwd     //    0  1      
grep -e "root;sshd"      
grep -e ‘root’ -e ‘sshd’ /etc/passwd      //  root sshd  ,-e        
grep ’[^a-z]ae’ /etc/passwd        //  ae          
grep ’^[a-z]ae’ /etc/passwd        //  ae         
 :         ,    [a-z],    [A-Z],  [0-9]
grep ’o*’ /etc/passwd            //      (        ,       )
 :oo*        o  (   o    ,   o   0    )  egrep  
egrep '0+' /etc/passwd     //        0  
egrep ‘(root|ntp)’ /etc/passwd    //    root  nto  
egrep ‘ro?t’ /etc/passwd     //  rt  rot  
egrep -v ‘^$|^#’ /etc/passwd    //         #    ,      #     ,          sed    
sed       ,      ,           ,   ,  ,   
                      
      shell  ,          
sed          sed    :
sed -e ‘    ’   1   2...   :-e, ’    ’  ”;”       
sed -n -e ‘    ’  1   2...   -n        
sed -i -e ‘    ’   1   2...   -i:    ,         sed    :
      :[  1,[  2]]  [  ]
“  ”,   、     、$,            
“  ”,   p、d、s、r、w、i 
“  ”,   g,               (g  )
    :
p:      
d:      
s:    ,  :”   s/    /    /g”
r:      
w:     
i:  ,             
      :
  sed -n ‘p’ /etc/passwd    //         cat
  sed -n ‘-7p’ /etc/passwd   //  7     
  sed -n ‘$p’ /etc/passwd    //       
  sed -n ‘1,7{p;n}’ /etc/passwd   // 1~7        
  sed -n ‘1,7{n;p}’ /etc/passwd   // 1~7        
sed -n ‘1,+4p’ /etc/passwd    //  1 ,  4     
sed -n ‘/root/p’ /etc/passwd    //     root      
sed -n ‘10,/nom/p’ /etc/passwd   //   10       nom     
sed -nr ‘/ro{1,}t/p’ /etc/passwd   //     1     0, -r               rot root
sed -n’/root\|ntp/p’ /etc/passwd    //    root  ntp    :        ,         ”\”
sed -n ‘/root/=’ /etc/passwd      //   root        ,“=”      
sed -e ‘5q’ /etc/passwd          //          ,q  
sed -e ‘5p ; 7p;9p’ /etc/passwd     //  5 、7 、9           :          :
sed ‘root/i admin:x:490:490::/:/sbin/nologin’ /etc/passwd   //   root        admin:x:490:490::/:/sbin/nologin
sed ‘/root/a admin:x:490:490::/:/sbin/nologin’ /etc/passwd     //   root       admin:x:490:490::/:/sbin/nologin
sed ‘3aADMIN’ /etc/passwd           //  3     ADMIN
        :
  sed ‘1d’ /etc/passwd             //     
  sed ‘$d’ /etc/passwd             //      
  sed ‘/^$/d’ /etc/passwd          //      
  sed ‘2,4d’ /etc/passwd           //   2 4 
  sed ‘/root/d’ /etc/passwd         //    root  ,   ”i”      
  sed ‘/^root/d’ /etc/passwd        //   root    
  sed ‘/nologin$/d’ /etc/passwd     //   nologin              :
  sed ‘s/root//g’ /etc/passwd       //       root         g       s:    
  sed ’/root/c admin:x:490:490::/:/sbin/nologin’ /etc/passwd      //   root      admin:x:490....
  sed -n ‘s/root/admin/2p’ /etc/passwd      //     2 root   admin
  sed ’/root/s/root/ROOT/g’ /etc/passwd       //  1~3     bin    BIN
  sed ‘s/$/ABC/’ /etc/passwd         //          ABC
  sed ‘s/^/#/’ /etc/passwd            //       # 
  sed ‘/root/s /^/#/’ /etc/passwd       //   root       # 
  sed ‘1c ABC’ /etc/passwd           //       ABC
  sed ‘y/root/ROOT/’ /etc/passwd       // root     ROOT       y:    
  sed ‘/1,10y/root/ROOT/’ /etc/passwd      //  1~10   root     ROOT           :
  sed ‘15,16 w out.txt’ test.txt   //   
  sed ‘5r /etc/reslov.conf’ test.txt         // /etc/reslov.conf              
  sed ‘1,5{H;d};$G’ test.txt             //  1~5           :H,      。G,       
  sed ‘/^ip/s/^/#/’ test.txt               //  ip         #
  sed ‘1,5H;15,16G’ test.txt
      :
  sed -ne ‘s/root/admin/’ -ne ‘s/bash/sh/p’ /etc/passwd     // root bash    
  sed -ne ‘s/root/admin/;s/bash/sh/p’ /etc/passwd        // root bash    
        :   -i       ,        
  sed -i ‘s/^/#/’ /etc/passwd                  //       # ,         awk    
awk             , sed  ,                    
    : 
awk    '      {    }’   1   2
awk -f        1   2
    :
      ,             ,                  ,                awk    
FS:            ,         。       :                -F
NF:           
NR:         (  )
$0:           
$n:       n   ( n )    :
awk -F: '{print $0,NF}' /etc/passwd       //         /etc/passwd          
df -hT |awk '{print $1,$6}'        // awk    df -hT     ,      ,     ,   1   6   
awk '{print $0}' /etc/passwd       //      
cat /etc/passwd  
grep ""  /etc/passwd  
sed -n 'p' /etc/passwd       //              :
awk 'NR==1,NR==3{print}' bfile       //   1  3   
awk 'NR==1||NR==3{print}' bfile      //   1 、 3   
awk '/^root/{print}' /etc/passwd        //   root    
awk '/nologin$/{print}' /etc/passwd      //   nologin    
awk '(NR>=1)&&(NR<=3){print}' /etc/passwd         //   1   3   
awk "(NR%2)==1{print}' /etc/passwd            //          
awk '(NR%2)==0{print}' /etc/passwd            //          
awk -F: '!($3<900)' /etc/passwd                //   3      900  ,“!”          awk    ,           “  ”,          ,     (>)、  (  awk -F: '{if($3>200)print $0}' /etc/passwd    //   3     200  
awk -F: '{max=($3>$4)?$3:$4;print max}' /etc/passwd    
 //   3        4     ,            max,                max
awk -F: '{max=($3>200)?$3:$1;print max}' /etc/passwd    //   3       200,   3       max,     1       max
   awk             ,              ,     ,     if...else     ,  if...else              
awk -F: '{print NR,$0}' /etc/passwd      //         ,        ,NR  1
awk -F":" '$3<5{print $1 $3}'/etc/passwd     //   3   5  1   3   
awk -F ":" '($1~"root")&&(NF==7){print $1,$3}'/etc/passwd      //    7   ,   1      root   1  2    
awk -F":" 'NR==3,NR==7{print $1,$7}' /etc/passwd       //   3   7           1   7    
           :
awk -F: '/^root/{print "Hi," $1}' /etc/passwd        //         , root       ,      “Hi,”
awk '{print $1"--"$3}' 6.txt   //                             
awk -F":" '$7~"/bash"{print $1}' /etc/passwd      //        7      /bash    1   
awk -F':''{print $1":"$2":"$3":"$4}' /etc/passwd    //       ,        ,/etc/passwd    4   
awk -F":" '{print $1,$3}' /etc/passwd               //           1   3 
awk 'BEGIN{FS=":"} {print $1,$3}' /etc/passwd       //           1   3 
awk 'BEGIN{X=0};/\/bin\/bash$/{x++};END{printx}' /etc/passwd       //   /bin/bash        awk    :    BEGIN{}    ,               ,    NF、NR、$0、$1       , s  '     {    }’;    END{}  
         :
date |awk '{print "Month:"$2"
Year:"$6}'        //      2       Month:,     6       Year
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java에서 정규 표현식으로 html 태그 제거자바에서 정규 표현식은 html의 라벨을 제거하는데 주요 목적은 더욱 정확한 내용을 표시하는 것이다. 예를 들어 얼마 전에 블로그에서 글을 발표하는 것과 같은 기능을 했다. 편집기에서 내용을 입력하면 스타일 라벨도 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.