Linux下的sed流编辑器命令详解
sed是stream editor的简称,也就是流编辑器。它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。
使用语法
sed命令的使用规则是这样的:
sed [option] 'command' input_file
- -n 使用安静(silent)模式(想不通为什么不是-s)。在一般sed的用法中,所有来自stdin的内容一般都会被列出到屏幕上。但如果加上-n参数后,则只有经过sed特殊处理的那一行(或者动作)才会被列出来;其中option是可选的,常用的option有如下几种:
- -e 直接在指令列模式上进行 sed 的动作编辑;
- -f 直接将 sed 的动作写在一个文件内,
-f filename则可以执行filename内的sed命令; - -r 让sed命令支持扩展的正则表达式(默认是基础正则表达式);
-i 直接修改读取的文件内容,而不是由屏幕输出。
常用的命令有以下几种:
a \: append即追加字符串, a \的后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选择的行的后面都加上字符串s;
- c \: 取代/替换字符串,c \后面跟上字符串s(多行字符串可以用\n分隔),则会将当前选中的行替换成字符串s;
- d: delete即删除,该命令会将当前选中的行删除;
- i \: insert即插入字符串,i \后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选中的行的前面都插入字符串s;
- p: print即打印,该命令会打印当前选择的行到屏幕上;
- s: 替换,通常s命令的用法是这样的:
1,2s/old/new/g,将old字符串替换成new字符串
动作说明: [n1[,n2]]function
n1, n2 :不见得会存在,一般代表『选择进行动作的行数』,举例来说,如果我的动作是需要在 10 到 20 行之间进行的,则『 10,20[动作行为] 』
命令示例
假设有一个本地文件test.txt,文件内容如下:
[root@master rh]# cat test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
end
a命令
[root@master rh]# sed '1a \add one' test.txt
this is first line
add one
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
end
本例命令部分中的1表示第一行,同样的第二行写成2,第一行到第三行写成1,3,用$表示最后一行,比如2,$表示第二行到最后一行中间所有的行(包含第二行和最后一行)。
本例的作用是在第一行之后增加字符串”add one”,从输出可以看到具体效果。
[root@master rh]# sed '1,$a \add one' test.txt
this is first line
add one
this is second line
add one
this is third line
add one
this is fourth line
add one
this fifth line
add one
happy everyday
add one
end
add one
本例表示在第一行和最后一行所有的行后面都加上”add one”字符串,从输出可以看到效果。
[root@master rh]# sed '/first/a \add one' test.txt
this is first line
add one
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
end
本例表示在包含”first”字符串的行的后面加上字符串”add one”,从输出可以看到第一行包含first,所以第一行之后增加了”add one”
[root@master rh]# sed '/^ha.*day$/a \add one' test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
add one
end
本例使用正则表达式匹配行,^ha.*day$表示以ha开头,以day结尾的行,则可以匹配到文件的”happy everyday”这样,所以在该行后面增加了”add one”字符串。
i命令
i命令使用方法和a命令一样的,只不过是在匹配的行的前面插入字符串,所以直接将上面a命令的示例的a替换成i即可,在此就不啰嗦了。
c命令
[root@master rh]# sed '$c \add one' test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
happy everyday
add one
本例表示将最后一行替换成字符串”add one”,从输出可以看到效果。
[root@master rh]# sed '4,$c \add one' test.txt
this is first line
this is second line
this is third line
add one
本例将第四行到最后一行的内容替换成字符串”add one”。
[root@master rh]# sed '/^ha.*day$/c \replace line' test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
replace line
end
d命令
[root@master rh]# sed '/^ha.*day$/d' test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
end
本例删除以ha开头,以day结尾的行。
[root@master rh]# sed '4,$d' test.txt
this is first line
this is second line
this is third line
本例删除第四行到最后一行中的内容。
p命令
[root@master rh]# sed -n '4,$p' test.txt
this is fourth line
this fifth line
happy everyday
end
本例在屏幕上打印第四行到最后一行的内容,p命令一般和-n选项一起使用。
[root@master rh]# sed -n '/^ha.*day$/p' test.txt
happy everyday
本例打印以ha开始,以day结尾的行。
s命令
实际运用中s命令式最常使用到的。
[root@master rh]# sed 's/line/text/g' test.txt
this is first text
this is second text
this is third text
this is fourth text
this fifth text
happy everyday
end
本例将文件中的所有line替换成text,最后的g是global的意思,也就是全局替换,如果不加g,则只会替换本行的第一个line。
[root@master rh]# sed '/^ha.*day$/s/happy/very happy/g' test.txt
this is first line
this is second line
this is third line
this is fourth line
this fifth line
very happy everyday
end
本例首先匹配以ha开始,以day结尾的行,本例中匹配到的行是”happy everyday”这样,然后再将该行中的happy替换成very happy。
[root@master rh]# sed 's/\(.*\)line$/\1/g' test.txt
this is first
this is second
this is third
this is fourth
this fifth
happy everyday
end
这个例子有点复杂,先分解一下。首先s命令的模式是s/old/new/g这样的,所以本例的old部分即\(.*\)line$,sed命令中使用\(\)包裹的内容表示正则表达式的第n部分,序号从1开始计算,本例中只有一个\(\)所以\(.*\)表示正则表达式的第一部分,这部分匹配任意字符串,所以\(.*\)line$匹配的就是以line结尾的任何行。然后将匹配到的行替换成正则表达式的第一部分(本例中相当于删除line部分),使用\1表示匹配到的第一部分,同样\2表示第二部分,\3表示第三部分,可以依次这样引用。比如下面的例子:
[root@master rh]# sed 's/\(.*\)is\(.*\)line/\1\2/g' test.txt
this first
this second
this third
this fourth
th fifth
happy everyday
end
正则表达式中is两边的部分可以用\1和\2表示,该例子的作用其实就是删除中间部分的is。
Linux下的sed流编辑器命令详解的更多相关文章
- Linux下的tar压缩解压缩命令详解
转载自http://www.cnblogs.com/qq78292959/archive/2011/07/06/2099427.html tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压 ...
- Linux下的tar压缩解压缩命令详解(转)
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- 【Linux命令】Linux下的tar压缩解压缩命令详解(转)
tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...
- Linux下的tar压缩解压缩命令详解(转)
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- Linux下的设置静态IP命令详解
网络配置的配置文件在/etc/sysconfig/network-scripts/下,文件名前缀为ifcfg-后面跟的就是网卡的名称,可以通过双TAB键查看然后编辑,也可以使用ifconfig查看,也 ...
- Linux下的awk文本分析命令详解
一.简介 awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入.一个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是linux/ ...
- linux下reboot和shutdown关机命令详解
我 们在操作Linux v/服务器的时候肯定会有需要重启系统,或者关闭系统等操作.有些用户是直接到VPS主机商家面板上操作的,这样一来比较麻烦,二来有些面板还不易于使用 容易导致面板卡死.所以最好的方 ...
- Linux下的JDK安装rpm命令详解
1. 安装程序 #rpm -ivh jdk-7u79-linux-x64.rpm 出现安装协议等,按接受即可. 2.设置环境变量. #vi /etc/profile JAVA_HOME=/usr/ja ...
- Linux进程实时IO监控iotop命令详解
介绍 Linux下的IO统计工具如iostat, nmon等大多数是只能统计到per设备的读写情况, 如果你想知道每个进程是如何使用IO的就比较麻烦. iotop 是一个用来监视磁盘 I/O 使用状况 ...
随机推荐
- 【笨嘴拙舌WINDOWS】计时器精度
WINDOWS的大多数系统并非实时操作系统,所以不能规定计算机在某个精确到纳秒的时间让计算机做某项任务,如果规定了时间WINDOWS也将需要在完成了线程调度后,经行任务执行! 也就是说,如果你的应用程 ...
- BZOJ 1078 斜堆
感谢MATO大神的博客 http://www.cppblog.com/MatoNo1/archive/2013/03/03/192131.html 注意细节. #include<iostream ...
- LA 3516 Exploring Pyramids (记忆化搜索)
题意 给定一个DFS序列,问能有多少树与之对应. 思路 设输入序列为S,dp(i, j)为子序列Si, Si+1, --, Sj对应的树的个数,则边界条件为d(i, i) = 1,且Si != Sj时 ...
- erl_0013 erlang 带参数模块 parameterized modules are no longer supported
code: -module(mod_test, [Name]). -export([show/0]). show() -> io:format("show:~p~n",[Na ...
- *ecshop 首页促销价显示倒计时
1.打开includes/lib_goods.php 找到 get_promote_goods()函数部 在(注意:位置别找错了,大概在394行位置) $goods[$idx]['url'] = bu ...
- HBase 系统架构
HBase是Apache Hadoop的数据库,能够对大型数据提供随机.实时的读写访问.HBase的目标是存储并处理大型的数据.HBase是一个开源的,分布式的,多版本的,面向列的存储模型.它存储的是 ...
- webstorm安装破解版
破解方法: 现在有个比较简单的注册方法.注册时选择“License server”输入“http://15.idea.lanyus.com/”点击“OK”即可快速激活JetBrains系列产品”
- RequireJS入门(一) 转
RequireJS由James Burke创建,他也是AMD规范的创始人. RequireJS会让你以不同于往常的方式去写JavaScript.你将不再使用script标签在HTML中引入JS文件,以 ...
- 删除github.com上repository(仓库)的方法
第一步:打开http://github.com,看到右侧仓库列表.第二步:假设要删除“HiTop”这个参考,点击对应仓库进入详细页面之后,在右侧会看到“Settings”入口. 第三步:进入设置页面之 ...
- 锋利的jQuery读书笔记---jQuery中Ajax--序列化元素、Ajax全局事件
serialize()方法: 作用于一个jQuery对象,它能够将DOM元素内容序列化为字符串,用于Ajax请求. <!DOCTYPE html> <html> <hea ...