SED详解

sed并不是直接对源文件进行处理,而是每次从文件中读取一行,复制出来一份放在自己的模式空间,如果能匹配到pattern,则对结果进行处理,即交给保持空间,如果不能匹配,则不进行处理。

sed:过滤或者转换文本,使用方法:

sed [OPTION]… {script-only-if-no-other-script} [input-file]…

script:地址定界编辑命令

常用选项:

-n:不输出模式空间中的内容(处理过程)

-e:实现多点编辑,即可以使用多个命令

-f:/PATH/TO/SED_SCRIPT_FILE,指定脚本文件,每行一个命令

-r:支持使用扩展正则表达式

-i:直接编辑源文件

地址定界

1、空地址:对全文进行处理;

2、单地址:

​ #:指定行

​ /pattern/:被次模式匹配到的每一行

3、地址范围:

​ #,#:

​ #,+#:

​ #,/pattern/

​ /pattern1/,/pattern2/

​ $最后一行

4、步进:~

​ 1~2:所有奇数行

​ 2~2:所有偶数行

编辑命令

删除:d

[root@Hank ~]# cat test.txt
Love makes the world go around
It is important for us to learn to love as the first class
Only when you know how to love than you will be a real man in this world
Hello,Good morning,lady and gentleman,
How do you do,glod to meet you,Im Zhao Ye,
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English
Love brings us warmth in the fearful coldness

删除第1-5行:

[root@Hank ~]# sed '1,5d' test.txt
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English
Love brings us warmth in the fearful coldness

删除以Love开头的行:

[root@Hank ~]# sed '/^Love/d' test.txt
It is important for us to learn to love as the first class
Only when you know how to love than you will be a real man in this world
Hello,Good morning,lady and gentleman,
How do you do,glod to meet you,Im Zhao Ye,
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English

打印:p

显示奇数行:

[root@Hank ~]# sed '1~2p' test.txt
Love makes the world go around
Love makes the world go around
It is important for us to learn to love as the first class
Only when you know how to love than you will be a real man in this world
Only when you know how to love than you will be a real man in this world
Hello,Good morning,lady and gentleman,
How do you do,glod to meet you,Im Zhao Ye,
How do you do,glod to meet you,Im Zhao Ye,
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English
Love brings us warmth in the fearful coldness
Love brings us warmth in the fearful coldness

通过输出发现符合条件的行被输出了两次,这是由于默认显示模式空间的内容,而匹配处理后,再次输出,所以显示了两次,要想去掉处理过程,需要加-n参数禁止显示处理过程:

[root@Hank ~]# sed -n '1~2p' test.txt
Love makes the world go around
Only when you know how to love than you will be a real man in this world
How do you do,glod to meet you,Im Zhao Ye,
Love brings us warmth in the fearful coldness

追加:a

a\text,在指定行后面追加文本text,支持使用\n实现多行追加

在H\L开头的行后面追加一行文本:

[root@Hank ~]# sed  '/^[HL]/a\***************' test.txt
Love makes the world go around
***************
It is important for us to learn to love as the first class
Only when you know how to love than you will be a real man in this world
Hello,Good morning,lady and gentleman,
***************
How do you do,glod to meet you,Im Zhao Ye,
***************
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English
Love brings us warmth in the fearful coldness
***************

插入:i

i\text,在指定行前面插入text,支持使用\n实现多行追加

在第一行前追加两行文本

[root@Hank ~]# sed '1i\This is a new text.\nThis is another new line' test.txt
This is a new text.
This is another new line
Love makes the world go around
It is important for us to learn to love as the first class
Only when you know how to love than you will be a real man in this world
Hello,Good morning,lady and gentleman,
How do you do,glod to meet you,Im Zhao Ye,
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English
Love brings us warmth in the fearful coldness

替换:c

c\text,把匹配到的行替换为text

把含逗号的行替换掉

[root@Hank ~]# sed   '/,/c\**************' test.txt
Love makes the world go around
It is important for us to learn to love as the first class
Only when you know how to love than you will be a real man in this world
**************
**************
**************
Love brings us warmth in the fearful coldness

另存:w

w /PATH/TO/FILE:保存模式空间匹配到的行到指定的文件中

把以H开头的行保存到/tmp/newtext中:

[root@Hank ~]# sed -n  '/^H/w /tmp/newtext' test.txt
[root@Hank ~]# cat /tmp/newtext
Hello,Good morning,lady and gentleman,
How do you do,glod to meet you,Im Zhao Ye,

读取:r

r /PATH/TO/FILE:读取指定文件的内容至当前文件被模式空间匹配到的行处

把/tmp/newtext追加到第3行:

[root@Hank ~]# sed '3r /tmp/newtext' test.txt
Love makes the world go around
It is important for us to learn to love as the first class
Only when you know how to love than you will be a real man in this world
Hello,Good morning,lady and gentleman,
How do you do,glod to meet you,Im Zhao Ye,
Hello,Good morning,lady and gentleman,
How do you do,glod to meet you,Im Zhao Ye,
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English
Love brings us warmth in the fearful coldness

行号 :=

为匹配到的行打印行号:

[root@Hank ~]# sed '2~2=' test.txt
Love makes the world go around
2
It is important for us to learn to love as the first class
Only when you know how to love than you will be a real man in this world
4
Hello,Good morning,lady and gentleman,
How do you do,glod to meet you,Im Zhao Ye,
6
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English
Love brings us warmth in the fearful coldness

取反:!

显示不是以s结尾的行:

[root@Hank ~]# sed -n '/s$/!p' test.txt
Love makes the world go around
Only when you know how to love than you will be a real man in this world
Hello,Good morning,lady and gentleman,
How do you do,glod to meet you,Im Zhao Ye,
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English

查找替换:s///

其分隔符可以自行指定,常用的有s@@@,s###等;

替换标记:

g:全局替换

w /PATH/TO/FILE:将替换成功的行保存至文件

p:只显示替换成功的行

把所有行的末尾同意替换为句号:

首先看一下各行结尾

[root@Hank ~]# cat -A test.txt
Love makes the world go around$
It is important for us to learn to love as the first class$
Only when you know how to love than you will be a real man in this world $
Hello,Good morning,lady and gentleman,$
How do you do,glod to meet you,Im Zhao Ye,$
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English $
Love brings us warmth in the fearful coldness$

发现第3行,第5行结尾有空格,4、5行以逗号结尾,其他行结尾没有标点,执行替换:

[root@Hank ~]# sed -r 's/ $|,$|$/./g' test.txt
Love makes the world go around.
It is important for us to learn to love as the first class.
Only when you know how to love than you will be a real man in this world.
Hello,Good morning,lady and gentleman.
How do you do,glod to meet you,Im Zhao Ye.
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English.
Love brings us warmth in the fearful coldness.

获取文件所在目录:

[root@Hank ~]# echo "/var/log/messages"|sed 's/[^/]\+$//'
/var/log/
[root@Hank ~]# echo "/var/log/messages/" | sed 's@[^/]\+/\?$@@'
/var/log/

高级编辑命令

h:把模式空间的内容覆盖至保存空间

H:把模式空间的内容追加到保存空间

g:把保持空间的内容覆盖至模式空间

G:把保持空间的内容追加至模式空间

x:把模式空间的内容与保持空间的内容互换

n:覆盖读取匹配的行的下一行至模式空间中

N:追加读取匹配到的行的下一行至模式空间中

d:删除模式空间中的行

D:删除多行模式空间中的所有行

显示偶数行:

[root@Hank ~]# sed -n 'n;p' test.txt
It is important for us to learn to love as the first class
Hello,Good morning,lady and gentleman,
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English

删除偶数行:

[root@Hank ~]# sed 'n;d' test.txt
Love makes the world go around
Only when you know how to love than you will be a real man in this world
How do you do,glod to meet you,Im Zhao Ye,
Love brings us warmth in the fearful coldness

按行逆序显示文件内容:相当于tail

[root@Hank ~]# sed '1!G;h;$!d' test.txt
Love brings us warmth in the fearful coldness
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English
How do you do,glod to meet you,Im Zhao Ye,
Hello,Good morning,lady and gentleman,
Only when you know how to love than you will be a real man in this world
It is important for us to learn to love as the first class
Love makes the world go around

显示最后一行:相当于tail -1

[root@Hank ~]# sed '$!d' test.txt
Love brings us warmth in the fearful coldness

显示文件后两行:相当于tail -2

[root@Hank ~]# sed '$!N;$!D' test.txt
Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English
Love brings us warmth in the fearful coldness

删除原来的所有空行,并在每行下填充一个空白行:

[root@Hank ~]# sed '/^$/d;G' test.txt
Love makes the world go aroundIt is important for us to learn to love as the first classOnly when you know how to love than you will be a real man in this world Hello,Good morning,lady and gentleman,How do you do,glod to meet you,Im Zhao Ye,Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English Love brings us warmth in the fearful coldness

在原有的每行下,都添加一个空白行,不删除原来的空行:

[root@Hank ~]# sed 'G' test.txt
Love makes the world go aroundIt is important for us to learn to love as the first classOnly when you know how to love than you will be a real man in this world Hello,Good morning,lady and gentleman,How do you do,glod to meet you,Im Zhao Ye,Im Studuing Xi Da Jie primary school of Jiuquan,my topic is: I love English Love brings us warmth in the fearful coldness

Linux中sed命令详解相关推荐

  1. Linux的sed命令详解大全

    Linux的sed命令详解大全 一.sed命令介绍 二.sed 的运行模式 三.sed的相关选项 四.sed基本用法 1.sed语法 2.sed的查看功能 ①查看passwd文件的第5到第8行内容 ② ...

  2. Linux中iptraf命令详解(IP局域网监控工具)

    2019独角兽企业重金招聘Python工程师标准>>> Linux中iptraf命令详解(IP局域网监控工具) 发布时间:2017-12-27 20:46:03   作者:佚名    ...

  3. linux中create命令详解,linux中 pmap 命令详解

    通过查看帮助,返回了如下信息: Usage: pmap [options] pid [pid ...] Options: -x, --extended show details -X show eve ...

  4. linux中mkdir命令含义,Linux中mkdir命令详解

    Linux中mkdir命令详解 linux mkdir用来创建目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1. 新建一个文件夹 one 2. 新建三个 ...

  5. linux中dd命令详解,Linux中DD命令详解

    Linux中DD命令详解 1.dd命令简介 功能:把指定的输入文件拷贝到指定的输出文件中,并且在拷贝过程中可以进行格式转换.可以用该命令实现DOS下的diskcopy命令的作用.先用dd命令把软盘上的 ...

  6. linux pmap命令,linux中 pmap 命令详解

    通过查看帮助,返回了如下信息: Usage: pmap [options] pid [pid ...] Options: -x,--extended show details -X show even ...

  7. linux中替换命令详解,linux中sed命令字符串替换的用法详解

    Linux系统中sed命令可以将字符串批量替换,省去了很多麻烦,下面由学习啦小编为大家整理了linux系统中sed命令字符串替换的用法详解,希望对大家有帮助! linux中sed命令字符串替换的用法详 ...

  8. linux 之sed命令详解

    sed命令详解 sed是什么 sed是linux文本处理三剑客之一,全称 StreamEDitor,非常著名的行编辑器,功能十分强大. sed的工作过程 sed处理文本时每次从文件复制出来一行,放在工 ...

  9. 【转】【Linux】sed命令详解

    sed命令详解 sed是stream editor的简称,也就是流编辑器.它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space ...

  10. linux 修改用户dir,linux中dir命令详解

    在linux系统下dir命令的功能和ls命令差不多,主要是用以查看目录.文件权限等详细信息.下面由学习啦小编整理了linux中dir命令的详细解释,希望对你有帮助. linux中dir命令的详细解释 ...

最新文章

  1. 如何強迫 .Net 應用程式輸出英文的例外訊息
  2. WinCVS与CVSNT简明使用手则
  3. android 切换排列,在运行时重新排序android线性布局?
  4. 对pca降维后的手写体数字图片数据分类_python机器学习API介绍13: 数据降维及主成分分析...
  5. pytorch 图像分割的交并比_Segmentation101系列-最简单的卷积网络语义分割(1)-PASCAL VOC图像分割...
  6. Django 表操作时 字段名为变量
  7. JAVA面试要点009---TimeUnit用法
  8. 转: android apk 防止反编译技术(1~5连载)
  9. 取消文件与svn服务器的关联
  10. 【通信】基于matlab Alamouti空频编码【含Matlab源码 801期】
  11. 【电子签章】HTML格式合同转化成PDF文件
  12. 2019 CUMCM ABC Notes
  13. origin 快捷键
  14. 2016 hitb-facebook-ctf capture-mexico-tls RSA-CRT-Attack
  15. 秘制祖传正宗四川麻辣烫锅底配方
  16. CodeForces-937D-Sleepy Game
  17. 编程猫平台 python-编程猫发布高中新课标产品 助力中小学python教育
  18. SuperData上线VR数据平台,做行业发展的“指明灯”
  19. javascript 中的onblur 事件
  20. 什么是DNS?DNS的作用?

热门文章

  1. Python:过程型考核
  2. can滤波 dsp_CAN dsp 28335 CAN通信使用例程,已测试,可以正常 ,欢迎交流学习 DSP program 267万源代码下载- www.pudn.com...
  3. 滴滴裁员2000人:老板辞退你,从来都不是因为钱
  4. 基于饥饿博弈搜索算法的函数寻优算法
  5. 封装802.1Q与 ISL的区别
  6. 亚马逊资源名称 (ARN) 和 AWS 服务命名空间
  7. java web 组态,Java:Eclipse中使用WTP开发Web项目
  8. 1062 最简分数 (20 分)
  9. python制作工资表_Python实用案例:一秒自动生成工资条。
  10. html怎么引用网页链接,网页中各种链接引用方法小结