基礎與延伸比較二

  1. 編輯測試檔 b.txt。
    [dywang@dywmac zzz]$ vim b.txt
    [dywang@dywmac zzz]$ cat b.txt
    boot
    345 bot
    root 390 boooot
    aoot sort 390 boooot
    coot booooooot
    roooooooot 390 xyzbt
    root 134not390 boooot
    
  2. 查詢 3 至 5 個連續字元 o,使用延伸表示時大括號不加倒斜線。
    [dywang@dywmac zzz]$ grep --color 'o\{3,5\}' b.txt 
    root 390 boooot
    aoot sort 390 boooot
    coot booooooot
    roooooooot 390 xyzbt
    root 134not390 boooot
    [dywang@dywmac zzz]$ egrep --color 'o{3,5}' b.txt
    root 390 boooot
    aoot sort 390 boooot
    coot booooooot
    roooooooot 390 xyzbt
    root 134not390 boooot
    
  3. 查詢 b 與 t 之間 1 個以上連續字元 o。
    [dywang@dywmac zzz]$ grep --color 'boo*t' b.txt 
    boot
    345 bot
    root 390 boooot
    aoot sort 390 boooot
    coot booooooot
    root 134not390 boooot
    [dywang@dywmac zzz]$ egrep --color 'bo+t' b.txt 
    boot
    345 bot
    root 390 boooot
    aoot sort 390 boooot
    coot booooooot
    root 134not390 boooot
    
  4. 查詢 b 與 t 之間 0 或 1 個字元 o。
    [dywang@dywmac zzz]$ grep --color 'bo\{0,1\}t' b.txt 
    345 bot
    roooooooot 390 xyzbt
    [dywang@dywmac zzz]$ egrep --color 'bo?t' b.txt 
    345 bot
    roooooooot 390 xyzbt