输入文件不会被修改,sed 只在模式空间中执行替换命令,然后输出模式空间的
内容。
文本文件 employee.txt

101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

1.用 Director 替换所有行中的 Manager

替换命令用到参数s 注意代码格式

[root@localhost /]# sed 's/Manager/Director/' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Director
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Director

2.只把包含 Sales 的行中的 Manager 替换为 Director

[root@localhost /]# sed '/Sales/s/Manager/Director/' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Director

3.全局标志 g

用大写 A 替换第一次出现的小写字母 a

[root@localhost /]# sed 's/a/A/' employee.txt
101,John Doe,CEO
102,JAson Smith,IT Manager
103,RAj Reddy,Sysadmin
104,AnAnd Ram,Developer
105,JAne Miller,Sales Manager

把所有小写字母 a 替换为大写字母 A

[root@localhost /]# sed 's/a/A/g' employee.txt
101,John Doe,CEO
102,JAson Smith,IT MAnAger
103,RAj Reddy,SysAdmin
104,AnAnd RAm,Developer
105,JAne Miller,SAles MAnAger

4.数字标志

把第二次出现的小写字母 a 替换为大写字母 A

[root@localhost /]# sed 's/a/A/2' employee.txt
101,John Doe,CEO
102,Jason Smith,IT MAnager
103,Raj Reddy,SysAdmin
104,Anand RAm,Developer
105,Jane Miller,SAles Manager

建立一下文件

vi substitute-locate.txt
locate command is used to locate files
locate command uses database to locate files
locate command can also use regex for searching

用刚才建立的文件,把每行中第二次出现的 locate 替换为 find

[root@localhost /]# sed 's/locate/find/2' substitute-locate.txt
locate command is used to find files
locate command uses database to find files
locate command can also use regex for searching

5.打印标志 p

只打印替换后的行

[root@localhost /]# sed -n 's/John/johnny/p' employee.txt
101,johnny Doe,CEO

把每行中第二次出现的 locate 替换为 find 并打印出来

[root@localhost /]# sed -n 's/locate/find/2p' substitute-locate.txt
locate command is used to find files
locate command uses database to find files

6.写标志w

标志 w 代表 write,当替换操作执行成功后,它把替换后的结果保存的文件。
把每行出现的 John 替换为 Johnny,只把替换后的内容写到 output.txt 中

[root@localhost /]# sed -n 's/John/Johnny/w output.txt' employee.txt
[root@localhost /]# cat output.txt
101,Johnny Doe,CEO

把每行第二次出现的 locate 替换为 find,把替换的结果保存到文件中,同时显示输入文件所有内容

[root@localhost /]# sed 's/locate/find/2w output.txt' substitute-locate.txt
locate command is used to find files
locate command uses database to find files
locate command can also use regex for searching
[root@localhost /]# cat output.txt
locate command is used to find files
locate command uses database to find files

7.忽略大小写标志 i

把 john 或 John 替换为 Johnny,但是文件中的John中的J是大写,所以用i参数忽略大小写

[root@localhost /]# sed 's/john/Johnny/i' employee.txt
101,Johnny Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

8.执行命令标志 e

如下图所示,e参数就是执行的作用 文本文件ls -l /etc/passwd e参数则是执行这条命令

[root@localhost /]# vi /files.txt
[root@localhost /]# cat /files.txt
/etc/passwd
/etc/group
[root@localhost /]# sed 's/^/ls -l/' files.txt
ls -l/etc/passwd
ls -l/etc/group
[root@localhost /]# sed 's/^/ls -l /e' files.txt
-rw-r--r--. 1 root root 1766 10月 24 15:15 /etc/passwd
-rw-r--r--. 1 root root 920 10月 24 15:15 /etc/group

给特定行加注释或者删除注释

[root@VM-0-13-centos home]# cat test
locate command is used to locate files
locate command uses database to locate files
locate command can also use regex for searching #  &符号表示替换字符串中被找到的部分, 这条命令是替换以locate开头的所有行
[root@VM-0-13-centos home]# sed 's/^locate/#&' test
sed: -e expression #1, char 12: unterminated `s' command
[root@VM-0-13-centos home]# sed 's/^locate/#&/' test
#locate command is used to locate files
#locate command uses database to locate files
#locate command can also use regex for searching # locate被标记为1 ,\1就是复制这个位置的内容  如果有 第二个 那么久\2就是复制第二个位置的内容

[root@VM-0-13-centos home]# sed 's!^#\(locate\)!\1!' test
locate command is used to locate files
locate command uses database to locate files
locate command can also use regex for searching
[root@VM-0-13-centos home]#

  

sed 替换命令 s 后面的三个分隔符可以指定为其他符号,不一定都要是"/",可以用 “|” ,“!”,“#” 等来分隔。

    sed -i '/pam_wheel.so use\_uid/ s|^#\(.*\)$|\1|' /etc/pam.d/su  #先模式匹配找到行,再用s替换掉注释

sed 添加注释和取消注释的简单例子

[root@localhost yum.repos.d]# cat Local.repo
[Local]
name=localyum
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
[root@localhost yum.repos.d]# sed -n 's/^en/#en/p' Local.repo           #这行是找到以en开头的行,替换en为#en
#enabled=1

 [root@localhost yum.repos.d]# sed -n 's/^#en/en/p' Local.repo           #这个同理取消注释
  enabled=1

参考链接:https://blog.csdn.net/weixin_43150761/article/details/83152456

sed 替换命令使用的更多相关文章

  1. sed替换命令

    sed替换命令 语法为: sed ' [ address-range | pattern-range ] s/original-string/replacement-string/[substitut ...

  2. linux:vi 替换命令

    转自:http://www.cnblogs.com/afant/archive/2009/03/11/1408745.html :s/^.*$/\L&/100 ##将100行内的小写转换成大写 ...

  3. vim替换命令

    转载:http://blog.csdn.net/glorin/article/details/6317098 替換(substitute) :[range]s/pattern/string/[c,e, ...

  4. VIM中的正则表达式及替换命令

    VIM中的正则表达式及替换命令 一.使用正则表达式的命令 使用正则表达式的命令最常见的就是 / (搜索)命令.其格式如下: /正则表达式 另一个很有用的命令就是 :s(替换)命令,将第一个//之间的正 ...

  5. sed高级命令

    所谓高级,主要是指这里将要提到的命令都能改变sed执行或者控制的流程顺序(sed通常都是一行被读入模式空间,并用脚本中的sed命令一个接一个的应用于那一行). 高级sed命令分成3个组: 1)处理多行 ...

  6. vi 替换命令“找不到模式”解决

    在linux vi编辑工具中使用替换命令操作时,会出现明明有匹配查找模式的数据.却报"找不到模式"问题. 原因是vi s///替换操作缺省针对行,若要生效,则须要将光标移动到指定行 ...

  7. 【转】vim替换命令

    vim替换命令 free:此文后面涉及了正则表达式,随便看了一下,觉得正则表达式有时间学一学对于在Linux下操作也是方便许多 替換(substitute) :[range]s/pattern/str ...

  8. Linux替换命令

    :s/^.*$/\L&/100 ##将100行内的小写转换成大写 vi/vim 中可以使用 :s 命令来替换字符串. :s/vivian/sky/ 替换当前行第一个 vivian 为 sky ...

  9. 【转】sed单行命令大全

    文本间隔:  # 在每一行后面增加一空行  sed G # 将原来的所有空行删除并在每一行后面增加一空行.  # 这样在输出的文本中每一行后面将有且只有一空行.  sed '/^$/d;G' # 在每 ...

随机推荐

  1. PHP的Mhash扩展函数的学习

    这次我们要学习的又是一个 Hash 加密扩展.不过这个扩展 Mhash 已经集成在了 Hash 扩展中.同时也需要注意的是,这个扩展已经不推荐使用了,我们应该直接使用 Hash 扩展中的函数来进行 H ...

  2. 链式调用+对象属性与遍历+this指向+caller/callee

    之前的作业: 提示: 在开发的时候尽量在函数内部将作用都给调用好,在外部就能够直接使用 链式调用: 正常这样是不行的,因为没有具体返回值:  return 具体的对象,这样的才是链式操作,jquery ...

  3. Jenkins持续集成体系 | 最完整的介绍及资料

    这篇文章是来给大家普及Jenkins知识的, Jenkins能解决什么问题, 有哪些应用场景, 为何要掌握Jenkins, 掌握Jenkins后有哪些好处, 弄懂Jenkins需要掌握哪些知识 不知道 ...

  4. django安装DjangoUeditor富文本

    环境: pycharm,django1.11,python2.7 第一种:直接 pip install DjangoUeditor,直接从网上安装到pycharm 由于是直接安装,ueditor.ht ...

  5. 启用 Spring-Cloud-OpenFeign 配置可刷新,项目无法启动,我 TM 人傻了(上)

    本篇文章涉及底层设计以及原理,以及问题定位,比较深入,篇幅较长,所以拆分成上下两篇: 上:问题简单描述以及 Spring Cloud RefreshScope 的原理 下:当前 spring-clou ...

  6. Ueditor Version 1.4.3.3 SSRF

    点以前挖的洞.Ueditor是支持获取远程图片,较为经典的进行限制url请求,但是可以通过DNS重绑定绕过其验证. 代码分析 一般请求的url如下,其中source为数组,值为图片地址: /edito ...

  7. 在Unity中渲染一个黑洞

    在Unity中渲染一个黑洞 前言 N年前观看<星际穿越>时,被其中的"卡冈图雅"黑洞所震撼.制作团队表示这是一个最贴近实际的黑洞效果,因为它是通过各种科学理论实现的.当 ...

  8. Dapr逐渐被点燃

    Dapr被点燃 Dapr的热度个人认为才刚刚热起来,9月份我写了Dapr + .NET Core实战一共10篇,从基础概念到简单的实战,但是有很多人感兴趣,具体表现在我个人维护的QQ群,人数从80人左 ...

  9. Go语言之Goroutine与信道、异常处理

    一.Goroutine Go 协程可以看做成一个轻量级的线程,Go 协程相比于线程的优势: Goroutine 的成本更低大小只有 2 kb 左右,线程有几个兆. Goroutine 会复用线程,比如 ...

  10. react之组件生命周期

    四个阶段 初始化 运行中 销毁 错误处理(16.3以后) 初始化 constructor static getDerivedStateFromProps() componentWillMount() ...