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.






<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(37) | 评论(0) | 转发(0) |
0

上一篇:初识sed和gwak

下一篇:nucleus plus代码学习

给主人留下些什么吧!~~
评论热议
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

习惯就好zz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值