zoukankan      html  css  js  c++  java
  • 5-1-4-shell:字符串处理进阶--sed篇

    处理方式:逐行处理

    处理类型:增删改查

    文件demo: test.txt

    this is a test str.
    is 1 is 2 is 3 is 4 is 5 is 6 is 7 is 8
    1234
    qwer
    
    
    -i 参数直接修改文件
    
    
    插入选中行下一行文本, 开始标识字符 a\, 前置为选中行索引
    sed '1aabcd' test.txt
    
    结果:
    "
    this is a test str. 
    abcd
    is 1 is 2 is 3 is 4 is 5 is 6 is 7 is 8
    1234
    qwer
    "
    插入选中行上一行文本, 开始标识字符 i\, 前置为选中行索引
    sed '1iabcd' test.txt
    
    结果:
    "
    abcd 
    this is a test str.
    is 1 is 2 is 3 is 4 is 5 is 6 is 7 is 8
    1234
    qwer
    " 

    删除指定一行, 标识字符 d, 前置为行索引

    sed '2d' test.txt
    
    结果:
    "
    this is a test str.
    1234
    qwer
    "

    删除指定多行, 标识字符 d, 前置为行索引区间

    sed '2,3d'  test.txt
    
    结果
    "
    this is a test str.
    qwer
    "

    删除指定行到末尾, 标识字符 d, 前置为行索引区间, $ 标识末尾

    sed '3,$d'  test.txt
    
    结果:
    "
    this is a test str.
    is 1 is 2 is 3 is 4 is 5 is 6 is 7 is 8
    "

    替换  开始符s/, 结束符 /p

    sed 's/is/at/g' test.txt
    
    结果:
    "
    that at a test str. 
    at 1 at 2 at 3 at 4 at 5 at 6 at 7 at 8
    1234
    qwer
    "

    查找包含字符的行, 参数-n , 结束符 /p
    sed -n '/r/p' test.txt

    结果: " 1 this is a test str. 4 qwer "
    
    
  • 相关阅读:
    linux 安装软件的方式
    git 基本操作
    交叉编译
    windows下 打印机打印操作类 VS2015
    VS2015 下 unicode 字符转换类
    C++ 多线程日志类的使用
    编译模板实例化
    C++中如何使用switch字符串
    linux下静态库与动态库
    jsoncpp 解码编码 中文为空 乱码问题
  • 原文地址:https://www.cnblogs.com/lamp-lrh/p/14674103.html
Copyright © 2011-2022 走看看