zoukankan      html  css  js  c++  java
  • sed

    测试文本内容:

    This is a Certificate Request file:

    It should be mailed to zawu@seu.edu.cn

    =========================================================
    Certificate Subject:

    /O=Grid/OU=GlobusTest/OU=simpleCA-seugrid1.seu.edu.cn/OU=seu.edu.cn/CN=globus

    The above string is known as your user certificate subject,and it uniquely identifies this user, $88
    To install this user certificate,please save this e-mail into the following file.

    /home/globus/.globus/usercert.pem

    -n的使用

    定位

    匹配元字符

    如果sed命令匹配的目标字符串中包含元字符,需要使用转义符""屏蔽其特殊意义

    sed -n '/./p' input

    使用元字符进行匹配($符)

     $在正则中表示行尾,但是在sed命令中却表示最后一行

    sed -n '$p' input

    结果:

        /home/globus/.globus/usercert.pem

    编辑

    插入文本 /i

    直接引用行号

    sed '1i hello' input  #在第一行之前插入一行,内容为hello

    模式匹配之后

    sed '/file:/i hello' input #所有匹配file:的行之前,添加一行内容为hello

     *(行)后追加使用a,原理相同,下面举两例,后省略:

    sed '1a hello' input
    sed '/file:/a we add this line' input

    修改文本 /c

    使用c标记

    sed '/file:/c we edit this line' input  #此行相当于被覆盖重写

    删除文本

    单行:sed '1d' input

    多行:sed '1,3d' input

    替换文本

    s/被替换的文本/替换文本/替换选项

    替换之后再打印修改的行

    补充(与模式、或模式):

    同时匹配ABC 和 123:

    sed -n '/ABC/{/123/p}'        
    awk '/ABC/&&/123/{ print $0 }'  
    grep -E '(ABC.*123|123.*ABC)'    

    匹配ABC 或 123:

    sed -n '/(ABC|123)/p'
    awk '/ABC/||/123/{ print $0 }'
    grep -E '(ABC|123)'egrep 'ABC|123'

    样式:

    /(A|B|C|D|...)/p

  • 相关阅读:
    若依问题解决(一)
    Java 将两个List转换为流合并List
    后端返回前端文本换行显示,只能在前端再转换
    Java Stream() 流根据对象属性去重
    vue 当前端传回后端,后端使用实体类接收数据显示报错
    js 中 getMonth() 获取的月份比现实少一个月
    vue连个数组对比
    JS链接跳转方法
    ElementUI--表格toggleRowSelection无法选中
    Linux 常用命令
  • 原文地址:https://www.cnblogs.com/tinaluo/p/15333616.html
Copyright © 2011-2022 走看看