zoukankan      html  css  js  c++  java
  • sed 命令

      

    disable用来匹配特定一行,然后替换yes为no
    sed -i '/disable/s/yes/no/g' tftp

    全文替换yes为no
    sed -i 's/yes/no/g' tftp

    每一行第一处的yes替换为no
    sed -i 's/yes/no/' tftp

    1. 在包含某个关键字的行上面插入一行文字,命令如下:
    sed -i '/hello/i\1234' testfile
    执行完命令后,会将i后面反斜杠后面的内容插入到包含hello关键字行的上面

    2. 在包含某个关键字的行下面插入一行文字,命令如下:
    sed -i '/hello/a\4567' testfile     【a后边的\可以不要
    执行完命令后,会将a后面反斜杠后面的内容插入到包含hello关键字行的下面

    sed -i '/^[client]/a\default-character-set=utf8' /etc/my.cnf.d/client.cnf
    执行完后,在行[client]下面插入default-character-set=utf8

    删除以a开头的行
    sed -i '/^a.*/d' tmp.txt

    替换匹配行:
    sed -i 's/^a.*/haha/g' tmp.txt

    sed命令常用到的两个选项:
    -i : 直接在文件上编辑 (edit files in place)
    -e[默认选项]:只在命令行输出,而文件不改变
    (add the script to the commands to be executed)
    注:使用sed命令可以使用 -i 或者 -e 选项(以下例子仅以-i举例)

    sed命令删除特定行号
    删除第N行
    sed -i 'Nd' filename

    删除第N~M行
    sed -i 'N,Md' filename # file的[N,M]行都被删除

    删除shell变量表示的行号(配合for等语句使用)
    sed -i "${var1},${var2}d" filename # 这里引号必须为双引号

    删除最后一行
    sed -i '$d' filename

    sed命令删除包含特定字符行
    删除包含"xxx"的行
    sed -i '/xxx/d' filename

    1.匹配到指定行,然后在上一行插入
    在/etc/config/wireless文件中匹配到'sta'字符串,然后在改行的上一行插入config wifi-iface 'ap'
    sed -i "/'sta'/aconfig wifi-iface 'ap'" /etc/config/wireless
    说明:
    i:表示上一行
    a:表示下一行
    2.匹配到指定行,然后插入多行
    在/etc/config/wireless文件中匹配到'sta'字符串,然后在该行的下一行插入如下内容:
    config wifi-iface 'ap'
    option device 'mt7628'
    sed -i "/'sta'/aconfig wifi-iface 'ap' option device 'mt7628' " /etc/config/wireless

    1.查找当前目录下所有以txt文件中包含123的数字都替换成5678
    find ./ -name "*.txt" -exec grep "123" {} ; -exec sed -i 's/123/5678/g' {} ;

    2.查找当前目录中的sdtc.ini,然后插入2行,后边的删除是要处理win和linux 的 问题
    find ./ -name "sdtc.ini" -exec sed -i "10i[keyboard] boottime_numlock="on" " {} ; -exec sed -i '13d' {} ;

  • 相关阅读:
    ebs R12 支持IE11
    reloc: Permission denied
    3.23考试小记
    3.21考试小记
    3.20考试小记
    3.17考试小记
    3.15考试小记
    3.13考试小记
    3.12考试小记
    3.10考试小记
  • 原文地址:https://www.cnblogs.com/goozgk/p/9471535.html
Copyright © 2011-2022 走看看