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 "
    
    
  • 相关阅读:
    MVC ORM 架构
    Kubernetes 第八章 Pod 控制器
    Kubernetes 第七章 Configure Liveness and Readiness Probes
    Kubernetes 第六章 pod 资源对象
    Kubernetes 第五章 YAML
    Kubernetes 核心组件
    Kubernetes 架构原理
    Kubernetes 第四章 kubectl
    Kubernetes 第三章 kubeadm
    yum 配置及yum 源配置
  • 原文地址:https://www.cnblogs.com/lamp-lrh/p/14674103.html
Copyright © 2011-2022 走看看