zoukankan      html  css  js  c++  java
  • sed编辑器基础

    一、 更多的替换选项
    ①替换标记
    1. root@localhost sed]# cat data4.txt
    2. This is a test of the test script.
    3. This is the second test of the test script.
    4. [root@localhost sed]# sed 's/test/trial/' data4.txt
    5. This is a trial of the test script.
    6. This is the second trial of the test script.

    替换格式:   s/pattern/replacement/flags
    4种可用的替换标记:
    数字:表明新闻本将替换第几处模式匹配的地方;
    g,表明新文本将会替换所有匹配的文本;
    p,表明原先行的内容要打印出来;
    w file,将替换的结果写到文件中。

    可以指定sed编辑器用新文本替换第几处模式匹配的地方。
    1. [root@localhost sed]# sed 's/test/trial/2' data4.txt
    2. This is a test of the trial script.
    3. This is the second test of the trial script.
    后面第二个test被替换掉了。

    用g替换标记,能替换全部
    1. [root@localhost sed]# sed 's/test/trial/g' data4.txt
    2. This is a trial of the trial script.
    3. This is the second trial of the trial script.

    p替换标记会只打印修改过的行,和-n选项(禁止输出)一起使用。
    1. [root@localhost sed]# cat data5.txt
    2. This is a test line.
    3. This is a different line.
    4. [root@localhost sed]# sed -n 's/test/trial/p' data5.txt
    5. This is a trial line.

    ②替换字符
    替换(/),可以用其他字符替换字符串分隔符
    1. [root@localhost sed]# sed -n 's!/bin/bash!/bin/csh!p' /etc/passwd
    2. root:x:0:0:root:/root:/bin/csh
    3. tq2440:x:500:500:tq2440:/home/tq2440:/bin/csh
    4. win:x:501:501::/home/win:/bin/csh

    二、使用地址
    sed有两种形式的行寻址:
    ①以数字形式表示行区间
    ②用文本模式来过滤出行
    [address] command

    1.数字方式的行寻址
    1. [root@localhost sed]# sed '2s/dog/cat/' data1.txt
    2. The quick brown fox jumps over the lazy dog.
    3. The quick brown fox jumps over the lazy cat.
    4. The quick brown fox jumps over the lazy dog.






    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    modesim仿真
    EP3C系列FPGA的JTAG检测不了,JTAG下载失败,AS可以下载,下载完成后不执行程序
    本机修改虚拟机linux中的代码文件
    linux中的diff命令
    php中的elseif和else if
    php将数据写入另外一个文件
    IE6下的png不透明问题
    cookie的封装
    php从接口获取数据转成可以用的数组或其他(含转换编码)
    如何让后加载的元素被一开始就有的css样式渲染成功(强制提升css优先级)
  • 原文地址:https://www.cnblogs.com/ch122633/p/7363284.html
Copyright © 2011-2022 走看看