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

    sed是流编辑器,依据特定的匹配模式,对文本逐行匹配,并对匹配进行特定处理。

    格式命令:sed [选项命令]  "/pattern/操作命令" file

    选项命令

    -e 表示用指定命令或者脚本来处理输入的文本文件
    -f 表使用指定的脚本文件爱你来处理输入的文本文件
    -h 帮助命令
    -n 标识仅仅显示处理的结果
    -i 直接编辑文本文件

    操作命令

    查询

    p 打印,如果同时指定行,表示打印指定行;如果不指定行,表示打印所有内容

    删除

    d 删除,删除选定的行

    增加

    a 增加,匹配到的行后追加内容

    i 插入,匹配到的行钱追加内容

    r 将后面指定文件的内容追加到匹配到的行后面

    w将匹配到的换行内容另存到其他文件

    修改
    c 替换,将选定行替换为指定内容


    s 替换,替换指定字符串
    y 字符转换

    parttern用法

    # sed -n 'p' test.txt  '输出所有内容,等同于 cat test.txt'
    # sed -n '3p' test.txt    '输出第 3 行'
    # sed -n '3,5p' test.txt    '输出第 3~5 行'
    # sed -n 'p;n' test.txt    '输出所有奇数行,n 表示读入下一行资料'
    # sed -n 'n;p' test.txt    '输出所有偶数行,n 表示读入下一行资料'
    # sed -n '1,5{p;n}' test.txt    '输出第 1~5 行之间的奇数行(第 1、3、5 行)'
    # sed -n '10,${n;p}' test.txt    '输出第 10 行至文件尾之间的偶数行'
    # sed -n '/^root/p' test.txt     '打印test.txt文件中以root开头的行'
    # sed -n '/^php/,/^java/p'       'test.txt 打印test.txt文件中以php开头的行到以java开头的行'
    # sed -n '4,/^php/p' test.txt     '打印test.txt文件中从第4行开始,直到以php开头的行'
    # sed -n '/the/p' test.txt '输出包含the 的行'
    # sed -n '4,/the/p' test.txt '输出从第 4 行至第一个包含 the 的行'
    # sed -n '/the/=' test.txt '输出包含the 的行所在的行号,等号(=)用来输出行号'
    # sed -n '/^PI/p' test.txt '输出以PI 开头的行'
    # sed -n '/[0-9]$/p' test.txt '输出以数字结尾的行'
    # sed -n '/<wood>/p' test.txt '输出包含单词wood 的行,<、>代表单词边界'

    d用法

    下面命令中 nl 命令用于计算文件的行数,结合该命令可以更加直观地查看到命令执行的结果

    # nl test.txt | sed '3d'    '删除第 3 行'
    # nl test.txt | sed '3,5d'    '删除第 3~5 行'
    # nl test.txt | sed '/cross/d'    '删除包含cross 的行'
    # nl test.txt | sed '/cross/! d'    '删除不包含cross 的行'
    # sed '/^[a-z]/d' test.txt    '删除以小写字母开头的行'
    # sed '/.$/d' test.txt    '删除以"."结尾的行'
    # sed '/^$/d' test.txt    '删除所有空行'
    # sed -e '/^$/{n;/^$/d}' test.txt    '删除重复的空行,即连续的空行只保留一个,效果与“cat -s test.txt”相同,n 表示读下一行数据'

     s用法

    使用 sed 命令进行替换操作时需要用到 s(字符串替换)、c(整行/整块替换)、y(字符转换)命令选项

    # sed 's/the/THE/' test.txt    '将每行中的第一个the 替换为 THE '
    # sed 's/l/L/2' test.txt    '将每行中的第 2 个l 替换为L '
    # sed 's/the/THE/g' test.txt    '将文件中的所有the 替换为THE'
    # sed 's/o//g' test.txt    '将文件中的所有o 删除(替换为空串)'
    # sed 's/^/#/' test.txt    '在每行行首插入#号'
    # sed '/the/s/^/#/' test.txt        '在包含the 的每行行首插入#号'
    # sed 's/$/EOF/' test.txt        '在每行行尾插入字符串EOF'
    # sed '3,5s/the/THE/g' test.txt    '将第 3~5 行中的所有the 替换为 THE'
    # sed '/the/s/o/O/g' test.txt    '将包含the 的所有行中的o 都替换为 O'
    cat user.txt | grep h |sed -n '1,3p' //在user.txt文件中;匹配带h的行 并且只显示1,3行
    cat user.txt | grep h | sed '$d' //删除最后一行记录
    cat user.txt | grep h |sed '2,3d' //在user.txt中显示带h的行;并且从结果中删掉2,3行的记录;只看第一行记录
    sed -i '1s/root/ROOT/'  file    //修改file文件中第1行中第一个root为ROOT
    sed -i '5,10s/test/TEST/g'  file   //修改file文件中第5行到第10行中所有的test改为TEST
  • 相关阅读:
    c++获取线程id
    一个基于c++的log库
    防止socket程序重启等待2MSL时间
    c++头文件循环引用
    Myeclipse 8.5 优化设置
    来道题 求解释
    MyEclipse常用设置笔记
    Ubuntu 学习笔记
    Linux 下常用命令
    Oracle 学习笔记 常用查询命令篇
  • 原文地址:https://www.cnblogs.com/wt645631686/p/8476771.html
Copyright © 2011-2022 走看看