sed 練習題

  1. 把/etc/passwd 複製到/root/test.txt,用sed打印所有行

     cp /etc/passwd /root/test.txt

     sed -n '1,$'p test.txt

  2. 打印test.txt的3到10行                                   sed -n '3,10'p  test.txt

  3. 打印test.txt 中包含 ‘root’ 的行                        sed -n '/root/'p test.txt

  4. 刪除test.txt 的15行以及以後所有行                  sed '15,$'d test.txt 

  5. 刪除test.txt中包含 ‘bash’ 的行                        sed '/bash/'d test.txt

  6. 替換test.txt 中 ‘root’ 爲 ‘toor’                        sed 's/root/toor/g' test.txt

  7. 替換test.txt中 ‘/sbin/nologin’ 爲 ‘/bin/login’ sed 's#/sbin/nologin#/bin/login#g' test.txt

  8. 刪除test.txt中5到10行中所有的數字              sed '5,10s/[0-9]//g' test.txt

  9. 刪除test.txt 中所有特殊字符(除了數字以及大小寫字母)sed 's/[^0-9a-zA-Z]//g' test.txt

  10. 把test.txt中第一個單詞和最後一個單詞調換位置 

               sed 's#(^[a-zA-Z][a-zA-Z]*)([^a-zA-Z].*)([^a-zA-Z])([a-zA-Z][a-zA-Z]*$)#\4\2\3\1#'

  11. 把test.txt中出現的第一個數字和最後一個單詞替換位置


    sed's#\([^0-9]*\)\([0-9]*\)\([^0-9].*\)\([^a-zA-Z]\)\([a-zA-Z]*$\)#\1\5\3\4\2#' test.txt

  12. 把test.txt 中第一個數字移動到行末尾

    sed 's#\([^0-9]*\)\([0-9]*\)\([^0-9].*$\)#\1\3\2#' test.txt

  13. 在test.txt 20行到末行最前面加 ‘aaa:’   sed '20,$s/^.*$/aaa:&/g' test.txt


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章