linux shell 脚本攻略学习19--sed命令详解
sed(意为流编辑器,英语“stream editor”的缩写)是Unix/linux常见的命令行程序。sed用来把文档或字符串里面的文字经过一系列编辑命令转换为另一种格式输出,即文本替换。sed通常用来匹配一个或多个正则表达式的文本进行处理。
输入sed --help查看帮助信息:
amosli@amosli-pc:~/learn/sed$ sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]... -n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
--follow-symlinks
follow symlinks when processing in place
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-r, --regexp-extended
use extended regular expressions in the script.
-s, --separate
consider files as separate rather than as a single continuous
long stream.
-u, --unbuffered
load minimal amounts of data from the input files and flush
the output buffers more often
--help display this help and exit
--version output version information and exit If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.
语法:
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
参数-实例:
测试文件:test.txt
amosli@amosli-pc:~/learn/sed$ cat test.txt
hi,this is sed command test file
linux world is so amazing you will like it!
1.简单的文本替换
常用格式:
sed 's/pattern/replace_string' file
#或者
cat file | sed 's/pattern/replace_string' file
其中pattern为模式,replace_string为替换词.即,将符合pattern的字符串替换为replace_string .
例:将will变为大写WILL
amosli@amosli-pc:~/learn/sed$ sed "s/will/WILL/" test.txt
hi,this is sed command test file
linux world is so amazing you WILL like it!
使用标准输入:
amosli@amosli-pc:~/learn/sed$ echo "this is test" | sed "s/test/TEST/"
this is TEST
2.-i参数,将替换结果应用于原文件
默认情况下sed不会修改原文件,因为式作原理为:sed编辑器逐行处理文件(或输入),并将结果发送到屏幕。具体过程如下:首先sed把当前正在处理的行保存在一个临时缓存区中(也称为模式空间),然后处理临时缓冲区中的行,完成后把该行发送到屏幕上。sed每处理完一行就将其从临时缓冲区删除,然后将下一行读入,进行处理和显示。处理完输入文件的最后一行后,sed便结束运行。sed把每一行都存在临时缓冲区中,对这个副本进行编辑,所以不会修改原文件。
amosli@amosli-pc:~/learn/sed$ sed -i 's/like/LOVE/' test.txt
amosli@amosli-pc:~/learn/sed$ cat test.txt
hi,this is sed command test file
linux world is so amazing you will LOVE it!
英文提示信息:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
3、关于后缀/g
/g默认表示替换所有符合条件的文本。
amosli@amosli-pc:~/learn/sed$ echo "test1 test2 test3 test4 " | sed "s/test/TEST/g"
TEST1 TEST2 TEST3 TEST4
/Ng表示从第N个符合条件的开始替换.
amosli@amosli-pc:~/learn/sed$ echo "test1 test2 test3 test4 " | sed "s/test/TEST/3g"
test1 test2 TEST3 TEST4
4、关于字符'/'
字符‘/’在sed命令中扮演着定界符的作用,'|'和':'的作用和'/'一样,可以做为定界符使用
如:
amosli@amosli-pc:~/learn/sed$ echo "test1 test2 test3 test4 " | sed "s:test:TEST:g"
TEST1 TEST2 TEST3 TEST4
amosli@amosli-pc:~/learn/sed$ echo "test1 test2 test3 test4 " | sed "s|test|TEST|g"
TEST1 TEST2 TEST3 TEST4
当定界符出现在样式内部中,可以使用前缀\对它进行转义,如:
amosli@amosli-pc:~/learn/sed$ echo "te/st1 te/st2 te/st3 te/st4 " | sed "s/test/TEST/g"
te/st1 te/st2 te/st3 te/st4
这样替换就失效了,使用前缀\进行转义
amosli@amosli-pc:~/learn/sed$ echo "te/st1 te/st2 te/st3 te/st4 " | sed "s/te\/st/TEST/g"
TEST1 TEST2 TEST3 TEST4
5.移除空白行
amosli@amosli-pc:~/learn/sed$ sed '/^$/d' test.txt
hi,this is sed command test file
linux world is so amazing
you will LOVE it!
/pattern/d 会移除匹配样式的行
^$行尾标记紧临表示空白行。
6.&代表已经匹配的字符串
amosli@amosli-pc:~/learn/sed$ echo this is a test | sed 's/\w\+/[&]/g'
[this] [is] [a] [test]
\w\+ 正则表达式表示匹配每一个单词,&对应于之前所匹配到的单词,使用[&]进行替换。
7.子串匹配字符口中\1
amosli@amosli-pc:~/learn/sed$ echo this is digit in a number | sed 's/digit \([0-9]\)/\1/'
this is in a number
这条命令将digit 7 替换为7,\(pattern\) 用于匹配子串,模式被包含在使用生命线转义过的()中。对于匹配到的第一个子串,其对应的标记是\1,匹配到的第二个子串为\2,以次类推。
8.组合多个表达式
amosli@amosli-pc:~/learn/sed$ cat test.txt
hi,this is sed command test file
linux world is so amazing you will LOVE it!
amosli@amosli-pc:~/learn/sed$ cat test.txt | sed 's/linux/LINUX/' | sed 's/sed/SED/'
hi,this is SED command test file
LINUX world is so amazing you will LOVE it!
也可以使用:
amosli@amosli-pc:~/learn/sed$ cat test.txt | sed "s/linux/LINUX/; s/sed/SED/"
hi,this is SED command test file
LINUX world is so amazing you will LOVE it!
格式为:
sed 'expression' | sed 'expression'
#or
sed 'expression;expression'
9.引用
定义变量:
amosli@amosli-pc:~/learn/sed$ txt=hello
引用变量:
amosli@amosli-pc:~/learn/sed$ echo "hello world" | sed "s/$txt/HELLO/g"
HELLO world
10.可选参数:
| 命令 | 功能 |
| a\ |
在当前行后添加一行或多行。多行时除最后一行外,每行末尾需用“\”续行 |
| c\ | 用此符号后的新文本替换当前行中的文本。多行时除最后一行外,每行末尾需用"\"续行 |
| i\ | 在当前行之前插入文本。多行时除最后一行外,每行末尾需用"\"续行 |
| d | 删除行 |
| h | 把模式空间里的内容复制到暂存缓冲区 |
| H | 把模式空间里的内容追加到暂存缓冲区 |
| g | 把暂存缓冲区里的内容复制到模式空间,覆盖原有的内容 |
| G | 把暂存缓冲区的内容追加到模式空间里,追加在原有内容的后面 |
| l | 列出非打印字符 |
| p | 打印行 |
| n | 读入下一输入行,并从下一条命令而不是第一条命令开始对其的处理 |
| q | 结束或退出sed |
| r | 从文件中读取输入行 |
| ! | 对所选行以外的所有行应用命令 |
| s | 用一个字符串替换另一个 |
| g | 在行内进行全局替换 |
| w | 将所选的行写入文件 |
| x | 交换暂存缓冲区与模式空间的内容 |
| y | 将字符替换为另一字符(不能对正则表达式使用y命令) |
其他选项:
选项 功能
-e 进行多项编辑,即对输入行应用多条sed命令时使用
-n 取消默认的输出
-f 指定sed脚本的文件名
元字符总结:
| 元字符 | 功能 | 示例 |
| ^ | 行首定位符 | /^my/ 匹配所有以my开头的行 |
| $ | 行尾定位符 | /my$/ 匹配所有以my结尾的行 |
| . | 匹配除换行符以外的单个字符 | /m..y/ 匹配包含字母m,后跟两个任意字符,再跟字母y的行 |
| * | 匹配零个或多个前导字符 | /my*/ 匹配包含字母m,后跟零个或多个y字母的行 |
| [] | 匹配指定字符组内的任一字符 | /[Mm]y/ 匹配包含My或my的行 |
| [^] | 匹配不在指定字符组内的任一字符 | /[^Mm]y/ 匹配包含y,但y之前的那个字符不是M或m的行 |
| \(..\) | 保存已匹配的字符 | 1,20s/\(you\)self/\1r/ 标记元字符之间的模式,并将其保存为标签1,之后可以使用\1来引用它。最多可以定义9个标签,从左边开始编号,最左边的是第一个。此例中,对第1到第20行进行处理,you被保存为标签1,如果发现youself,则替换为your。 |
| & | 保存查找串以便在替换串中引用 | s/my/**&**/ 符号&代表查找串。my将被替换为**my** |
| \< | 词首定位符 | /\<my/ 匹配包含以my开头的单词的行 |
| \> | 词尾定位符 | /my\>/ 匹配包含以my结尾的单词的行 |
| x\{m\} | 连续m个x | /9\{5\}/ 匹配包含连续5个9的行 |
| x\{m,\} | 至少m个x | /9\{5,\}/ 匹配包含至少连续5个9的行 |
| x\{m,n\} | 至少m个,但不超过n个x | /9\{5,7\}/ 匹配包含连续5到7个9的行 |
linux shell 脚本攻略学习19--sed命令详解的更多相关文章
- linux shell 脚本攻略学习12--文件权限详解,chmod命令详解,chown命令详解,chattr命令详解
文件权限详解 一.chmod命令详解 文件权限和所有权是Unix/Linux文件系统最显著的特征之一.linux中的每一个文件都与多种权限类型相关联,在这些权限中主要分类为3种: 用户(User)是文 ...
- linux shell 脚本攻略学习6-xargs详解
xargs是一条Unix和类Unix操作系统的常用命令.它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题. 例如,下面的命令: rm `find /path -type f` ...
- linux shell 脚本攻略学习20--awk命令入门详解
awk生于1977年,创始人有三个,分别为 Alfred Aho,Peter Weinberger, 和 Brian Kernighan,名称源于三个创始人的姓的首字母. 作用:处理文本文件. awk ...
- linux shell 脚本攻略学习16--wc命令详解,tree命令详解
在文本处理的工作中,统计文件的行数,单词数和字符数非常有用.而对于开发人员本身来说,统计LOC(line of code ,代码行数)是一件重要的工作.linux中有什么命令可以帮助我们做统计呢?没错 ...
- linux shell 脚本攻略学习14--head命令详解,tail命令详解
当要查看上千行的大文件时,我们可不会用cat命令把整个文件内容给打印出来,相反,我们可能只需要看文件的一小部分地内容(例如文件的前十行和后十行),我们也有可能需要打印出来前n行或后n行,也有可能打印除 ...
- linux shell 脚本攻略学习13--file命令详解,diff命令详解
一.file命令详解 find命令可以通过查看文件内容来找出特定类型的文件,在UNIX/ Linux系统中,文件类型并不是由文件扩展名来决定的(windows中却正是这么做的),file命令的目的是从 ...
- linux shell 脚本攻略学习11--mkdir和touch命令详解
一.创建目录(mkdir命令详解) amosli@amosli-pc:~/learn$ mkdir dir amosli@amosli-pc:~/learn/dir$ mkdir folder amo ...
- linux shell 脚本攻略学习9--rename命令详解
rename命令详解: 对文件重命名是常用的操作之一,一般对单个文件的重命名用mv命令,如: amosli@amosli-pc:~/learn/example$ ls abc.txt amosli@a ...
- linux shell 脚本攻略学习8---md5校验,sort排序,uniq命令详解
一.校验与核实 目前最为出名的校验技术是md5sum和sha1sum,它们对文件内容使用相应的算法来生成校验和. 举例: amosli@amosli-pc:~/learn$ md5sum text.t ...
- linux shell 脚本攻略学习7---tr命令详解
tr命令详解 什么是tr命令?tr,translate的简写,translate的翻译: [trænsˈleit] vi. 翻译, 能被译出 vt. 翻译, 解释, 转化, 转变为, 调动 在这里用到 ...
随机推荐
- 测试工具之Charles视频教程(更新中。。。)
应群里小伙伴学习需求,录制新版 Charles V4 系列教程,后续内容抽空更新,测试工具系列带你上王者...(ノ°ο°)ノ前方高能预警 链接:http://pan.baidu.com/s/1c16P ...
- 黄聪:Wordpress二级域名共享用户cookie出现错误解决方案及WP的Cookie机制
在若干年以前,我刚开始折腾Wordpress没多久的时候,就自己摸索过 多个Wordpress网站共享一份数据表的实现方法 .这种看起来好像很高大上的类SSO功能,能够给用户在多个网站之间提供快速.无 ...
- 关于http协议详解
Author :Jeffrey 引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前 ...
- XE6移动开发环境搭建之IOS篇(7):在Mac OSX 10.8中安装Xcode4.6.3(有图有真相)
网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 在安装Xcode前, ...
- delphi 调用c#dll
public interface iProduct { string Buy(); } [ClassInterface(ClassInterfaceType.None)] public class P ...
- Blackfin DSP(二):寄存器操作与GPIO
BlackfinDSP的寄存器是通过指针操作的,与51.ARM等MCU一样,通过“或”操作来置1,通过“与”操作清零. 在DSP上最简单的外设非IO口莫属,但是由于其功能强大,远非一般IO口可比,因此 ...
- OS X 禁止Android File Transfer自动启动
操作步骤 关闭Android File Manager 在Activity Manager中退出Android File Manager Agent进程 在Applications中,将Android ...
- WCF使用小结:(1)WCF接收HTTP POST数据的处理方法
在WCF 4.0中,为我们创建Restful API有了更好的支持.通过定义UriTemplate,WebInvoke就可以快速开发API接口. 这里我记录一下HTTP POST数据时要如何接收POS ...
- service对象
Service 对象 提供用于创建服务程序的一组工具 语法 Shell.Service[.property|method] 属性 Description 服务描述,仅限于 Windows 2000 及 ...
- PHP连接SQLServer
连接前配置系统: 1.检查文件 php5.2.5/ntwdblib.dll 默认下面有一个,不能连接再替换. 下载正确版本的 ntwdblib.dll (2000.80.194.0),地址: http ...