Linux学习之正则表达式sed

  1. 删除第2行

    nl /etc/passwd|sed '2d'

  2. 删除第3行到最后一行

    nl /etc/passwd|sed '3,$d'

  3. 在第2行后加上字符串drink tea

    nl /etc/passwd|sed '2a drink tea'

  4. 在第2行前加上字符串drink tea

    nl /etc/passwd|sed '2i drink tea'

  5. 在第2行后加入两行内容drink tea? drink beer?

    nl /etc/passwd|sed '2a drink tea?回车 drink beer?'

  6. 将2~5行内容取代为this is line 2-5

    nl /etc/passwd|sed '2,5c this is line 2-5'

  7. 列出第5~7行

    nl /etc/passwd|sed -n '5,7p'

  8. 从ifconfig信息中截取出本机IP

    ifconfig eth0|grep 'inet addr:'|sed 's/^.*addr://g'|sed 's/Bcast.*$//g'

  9. 直接替换文档内.为!

    sed -i 's/./!/g' regular_expression.txt

  10. 删除第4行,第6行取代为no six line

    cat /etc/passwd|sed -e '4d' -e '6c no six line' > passwd.new

原文地址:https://www.cnblogs.com/enginex/p/6802717.html