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 "
    
    
  • 相关阅读:
    mybatis-config.xml详解
    过滤器与拦截器
    Tomcat 部署web 项目
    Tomcat架构
    git stash
    AbstractQueuedSynchronizer 源码解读(转载)
    Kafka 转载
    Oracle数据库TNS详解
    Oracle建表知识全面详解
    Oracle高级教程
  • 原文地址:https://www.cnblogs.com/lamp-lrh/p/14674103.html
Copyright © 2011-2022 走看看