一、 更多的替换选项

①替换标记

  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.

sed编辑器基础的更多相关文章

  1. 自学Linux Shell18.1-sed编辑器基础特性

    点击返回 自学Linux命令行与Shell脚本之路 18.1-sed编辑器基础特性 linux世界中最广泛使用的两个命令行编辑器: sed gawk 1. sed概念 sed是stream edito ...

  2. shell编程之sed编辑器&gawk程序

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://twentyfour.blog.51cto.com/945260/560372 s ...

  3. sed命令基础

    sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space ...

  4. sed命令基础2

    我在sed命令基础里面说了一下sed的基础用法,sed还有一些高级用法,由于我也是在学习中,写的博客可能会有想不到的地方,有问题希望大家指出. sed的高级用法主要在于两个空间的使用,模式空间和保持空 ...

  5. shell编程学习笔记之sed编辑器

    在shell编程中,大多数处理的都是文本文件.对文本文件进行处理除了使用交互式文本编辑器(vi[m],gedit......)也可以使用另外一类:流编辑器. 流编辑器:使用预定义的编辑规则来对文本进行 ...

  6. Vim编辑器基础

    Vim编辑器基础 vi:Visual Interface vim:VI iMproved Vim模式 1.编辑模式(命令模式) 只能下达命令,不能键入字符 2.输入模式 键入字符 3.末行模式 左下角 ...

  7. shell——sed编辑器

    目录 一.sed编辑器 1.1.sed编辑器工作流程 读取: 执行: 显示: 1.2.格式 1.3.常用选项 1.4.常用操作 1.5.替换 一.sed编辑器 sed是一种流编辑器,流编辑器会在编辑器 ...

  8. 三剑客之sed编辑器 基操

    目录: 一.sed编辑器 二.打印内容 三.使用地址 四.删除行 五.替换 六.插入 一.sed编辑器 sed是一种流编辑器,流编辑器会在编辑器处理数据之前基于预先提供的一组规则来编辑数据流. sed ...

  9. Shell之sed编辑器

    Shell之sed编辑器 目录 Shell之sed编辑器 一.sed编辑器 1. sed编辑器概述 2. sed编辑器的工作流程 二.sed命令 1. 命令格式 2. 常用选项 3. 常用操作 三.操 ...

随机推荐

  1. 26 October in 614

    Practice tower 有 \(N\,(2\le N\le 600000)\) 块砖,要搭一个 \(N\) 层的塔,要求:如果砖 \(A\) 在砖 \(B\) 上面,那么 \(A\) 不能比 \ ...

  2. Autoresize UIView to fit subviews

    @interface UIView (resizeToFit) -(void)resizeToFitSubviews; -(void)resizeHightToFitSubviews; -(void) ...

  3. B. pSort

    题目链接: http://codeforces.com/problemset/problem/28/B 题意: 给一个n,原本有个1到n按顺序排列的序列,给一个序列问,在给一个数组,表示这个位置的数可 ...

  4. android ndk 编译 libevent

    1. 下载 libevent 2.1.8 版本 https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/ ...

  5. 转 LoadRunner错误处理函数

    在脚本的Run-time Settings中,可以设置在脚本运行过程中发生错误的处理方式.进入到Run-time Settings中,切换到Miscellaneous标签页,可以看到Error Han ...

  6. FastJSON 远程执行漏洞,速速升级!

    相信大家用 FastJSON 的人应该不少,居然有漏洞,还不知道的赶紧往下看,已经知道此漏洞的请略过-- 2019年6月22日,阿里云云盾应急响应中心监测到FastJSON存在0day漏洞,攻击者可以 ...

  7. ConcurrentHashMap(锁分段技术)

    线程不安全的HashMap     因为多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap.   效率低下的HashTab ...

  8. rope(平衡二叉树)

    题目描述: 链接:https://www.nowcoder.com/acm/contest/141/C来源:牛客网 Eddy likes to play cards game since there ...

  9. Python运行出现:ModuleNotFoundError: No module named 'logbook',解决办法

    ModuleNotFoundError: No module named 'xlrd' ModuleNotFoundError: No module named 'xlwt' AttributeErr ...

  10. 简易的富文本编辑器WangEditor

    网址http://www.wangeditor.com/ var E = window.wangEditor; var editor = new E('#editor') // 或者 var edit ...