linux sed文本

sed介绍
sed(stream editor)是一种非交互式的流编辑器,经过多种转换修改流经它的文本。默认状况下,sed不会改变原文件自己,而只是对流经sed命令的文本进行修改,并将修改后的结果打印到标准输出中。
sed处理文本时是以行为单位的,每处理完一行就当即打印出来,而后再处理下一行,直至全文处理结束。sed可作编辑动做包括删除、查找替换、添加、插入、从其余文件读入数据等。
 
sed命令使用的场景包括如下一些:
  • 常规编辑器编辑困难的文本。
  • 太过庞大的文本,使用常规编辑器难以胜任(如vi一个几百兆的文件)。
  • 有规律的文本修改,加快文本处理速度(好比说全文替换)。
 
sed命令的功能十分强大,因为sed自己的复杂度,以及和正则表达式的结合,使用sed命令很是难以掌握。只有不断的多读用,才能深入的理解和记住sed的功能。
 
sed经常使用的命令:
sed命令 做用
a 在匹配行后面加入文本
c 字符转换
d 删除行
D 删除第一行
i 在匹配行前面接入文本

hjavascript

复制模板块的内容到存储空间
H 追加模板块的内容到存储空间
g 将存储空间的内容复制到模式空间
G 将存储空间的内容追加到模式空间
n 读取下一个输入行,用下一个命令处理新的行
N 追加下一个输入行到模板块后并在两者间插入新行
p 打印匹配的行
P 打印匹配的第一行
q 退出sed
r 从外部文件中读取文本
w 追加写文件
匹配的逆
s/old/new 用new替换正则表达式old
= 打印当前行号

 

 

sed经常使用的参数:java

sed参数 做用
-e 多条件编辑
-h 帮助信息
-n 不输出不匹配的行
-f 指定sed脚本
-V 版本信息
-i 直接修改原文件

 

sed经常使用的正则表达式匹配:nginx

元字符 做用
^ 匹配行的开始。如:/^cat/匹配全部以cat开头的行
$ 匹配行的结束。如:/cat$/匹配全部以cat结尾的行
. 匹配任一非换行字符。如:/c.t/匹配c后接一个任意字符,而后是t
* 匹配零个或任意多个字符。如:/*cat/匹配一串字符后紧跟cat的全部行
[] 匹配指定范围内的字符。如:/[Cc]at/匹配cat和Cat
[^] 匹配指定范围外的任意单个字符。如:/[^A-Z]/匹配没有大写字母的行
\(..\) 保存匹配的字符。如:s/\(love\)able/\1rs/, loveable被替换成lovers
& 保存搜索字符用来替换其余字符。如:s/love/**&**/,love编程**love**
\< 锚定单词的开始。如:/\<cat/匹配包含以cat开头的单词的行
\> 锚定单词的结尾。如:/cat\>/匹配包含以cat结尾的单词的行
[x\{n\} 重复字符x,m次。如:/o\{5\}/匹配包含5个o的行
x\{m,\} 重复字符x,至少m次。如:/o\{5,\}/匹配至少有5个o的行
x\{n,m\} 重复字符x,至少m次,很少于n次。如:/o\{5,10\}/匹配5到10个o的行
 
 
 
 
下面列出一些sed基本的操做的例子
 
有这么一个文件(sed.txt):
[root@kurol ~]# cat sed.txt this is line 1, this is First line this is line 2, the Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line
 
使用sed修改文件流的方法以下:
sed [option] 'command' file #option 是sed能够接受的参数 #command 是sed的命令集(一共有25个) #使用-e参数和分号链接多编辑命令 #该参数自己只是sed的一个简单参数,表示将下一个字符串解析为sed编辑命令 #通常状况下能够忽略,可是当sed须要传递多个编辑命令时该参数就不能少了​


下面的例子演示了将this改成that的同时,还要讲line改成LINE,两个编辑命令前都要使用-e参数,若是有更多的编辑需求,以此类推web

[root@kurol ~]# sed -e 's/this/that/g' -e 's/line/LINE/g' sed.txt that is LINE 1, that is First LINE that is LINE 2, the Second LINE, Empty LINE followed that is LINE 4, that is Third LINE that is LINE 5, that is Fifth LINE​ 

 

使用分号(;)链接两个都编辑的命令,上面的命令用分号也可达到一样的效果:正则表达式

[root@kurol ~]# sed 's/this/that/g ; s/line/LINE/g' sed.txt that is LINE 1, that is First LINE that is LINE 2, the Second LINE, Empty LINE followed that is LINE 4, that is Third LINE that is LINE 5, that is Fifth LINE​

 

删除编程

使用d命令可删除指定的行:缓存

#将file的第一行删除后输出到屏幕
[root@kurol ~]# sed '1d' sed.txt this is line 2, the Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line

 

因为sed默认不修改原文件,若是但愿保存修改后的文件则须要用重定向:bash

sed '1d' sed.txt > saved_file​ 

若是想直接修改文件,使用 -i 参数,这样的方式不会有任何输出,而是直接修改了源文件编辑器

sed -i '1d' sed.txt​ 

删除指定范围的行 :this

 

删除1-3行:

[root@kurol ~]# sed '1,3d' sed.txt this is line 4, this is Third line this is line 5, this is Fifth line

 

删除第一行当最后一行:

[root@kurol ~]# sed '1,$d' sed.txt [root@kurol ~]# #清空了sed.txt文件

 

删除最后一行:

[root@kurol ~]# sed '$d' sed.txt this is line 1, this is First line this is line 2, the Second line, Empty line followed this is line 4, this is Third line

 

删除指定范围之外的行(只保留第五行):

[root@kurol ~]# sed '5!d' sed.txt this is line 5, this is Fifth line

 

 删除全部包含Empty的行:

[root@kurol ~]# sed '/Empty/d' sed.txt this is line 1, this is First line this is line 4, this is Third line this is line 5, this is Fifth line

 

删除空行:

[root@kurol ~]# sed '/^$/d' sed.txt this is line 1, this is First line this is line 2, the Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line​

 

查找替换

使用s命令可将查找到的匹配文本内容替换成新的文本

s命令用于替换文本

 

将每行第一个line替换成LINE:

 

[root@kurol ~]# sed 's/line/LINE/' sed.txt this is LINE 1, this is First line this is LINE 2, the Second line, Empty line followed this is LINE 4, this is Third line this is LINE 5, this is Fifth line

以上只是把每一行的第一个line被替换了,'s/old/new/' 默认状况下只替换第一次匹配到的内容.

 

将每行匹配到2个line,并改成LINE:

[root@kurol ~]# sed 's/line/LINE/2' sed.txt this is line 1, this is First LINE this is line 2, the Second LINE, Empty line followed this is line 4, this is Third LINE this is line 5, this is Fifth LINE 

 

s命令利用g选项,能够完成全部匹配值的替换(全文替换):

[root@kurol ~]# sed 's/line/LINE/g' sed.txt this is LINE 1, this is First LINE this is LINE 2, the Second LINE, Empty LINE followed this is LINE 4, this is Third LINE this is LINE 5, this is Fifth LINE

 

字符转换

使用y命令可进行字符转换,其做用为将一系列字符逐个地变换为另一系列字符,基本用法以下:

sed 'y/old/new/' file​ 

该命令会将file中的o转换为n、l转换成e、d转换成w

注意转换字符和被转换字符的长度要相等,不然sed没法执行

 

将数字1转换为A,2转换为B,4转换为D,5转换为E:

 

[root@kurol ~]# sed 'y/1245/ABDE/' sed.txt this is line A, this is First line this is line B, the Second line, Empty line followed this is line D, this is Third line this is line E, this is Fifth line

 

插入文本

使用i或a命令插入文本,其中i表明在匹配行以前插入,而a表明在匹配行以后插入

 

使用i在第二行前插入文本:

[root@kurol ~]# sed '2 i Insert' sed.txt this is line 1, this is First line Insert this is line 2, the Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line

 

使用a在第二行后插入文本:

 

[root@kurol ~]# sed '2 a Insert' sed.txt this is line 1, this is First line this is line 2, the Second line, Empty line followed Insert this is line 4, this is Third line this is line 5, this is Fifth line

 

在匹配行的上一行插入文本:

[root@kurol ~]# sed '/Second/i\Insert' sed.txt this is line 1, this is First line Insert this is line 2, the Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line

 

读入文本

使用r命令可从其余文件中读取文本,并插入匹配行以后

 

将/etc/passwd 中的内容读出放到sed.txt空号以后

[root@kurol ~]# sed '/^$/r /etc/passwd' sed.txt this is line 1, this is First line this is line 2, the Second line, Empty line followed root:x:0:0:root:/root:/bin/bash ...... nginx:x:498:499:Nginx web server:/var/lib/nginx:/sbin/nologin this is line 4, this is Third line this is line 5, this is Fifth line

 

 

打印

使用p命令可进行打印,这里使用sed命令时必定要加-n参数,表示不打印不要紧的行。从以前的例子能够看出,因为sed的工做原理是基于行的,所以每次都有大量的输出。但是这些输出中有一些咱们并不须要看到的,而只须要输出匹配的行或者处理过的行就行了。简单来讲,打印操做是删除操做的“逆操做”。

 

打印出文件中指定的行:

[root@kurol ~]# sed -n '1p' sed.txt this is line 1, this is First line​

 

将the替换成THE,sed实际处理了第二行,其余几行因为没有匹配因此并未真正处理,可是sed的工做原理是基于流的,因此全部流过的行都打印出来了:

[root@kurol ~]# sed 's/the/THE/' sed.txt this is line 1, this is First line this is line 2, THE Second line, Empty line followed this is line 4, this is Third line this is line 5, this is Fifth line

 

 

使用p命令,则只打印实际处理过的行,简化了输出(使用-n参数):

 

[root@kurol ~]# sed -n 's/the/THE/p' sed.txt this is line 2, THE Second line, Empty line followed

 

写文件

sed自己默认并不改写原文件,而只是对缓存区的文本作了修改并输出到屏幕。因此想保存文件,除了以前提到的两种方法外(使用重定向或-i参数),还可使用w命令将结果保存到外部指定文件。

[root@kurol ~]# sed -n '1,2 w output' sed.txt [root@kurol ~]# #这里没有任何输出,由于输出被重定向到output文件了 [root@kurol ~]# cat output this is line 1, this is First line this is line 2, the Second line, Empty line followed

 

sed脚本

在平日的工做中,须要按期对一些文件作分析操做,这种例行的工做每每有必定“标准化” 的操做,好比说先去除文件中全部的空行,而后再所有替换某些字符等,这种过程相似于生产线上程式化的流水做业。事实上,能够把这些动做静态化地写到某个文件中,而后调用sed命令并使用-f参数指定该文件,这样就能够将一系列动做“装载”并应用于指定文件中,这无疑加快了工做效率,这种文件就是sed脚本。

 

如,建立sed.rules脚本文件,该sed脚本的做用是将全文的this改成THAT,并删除全部空号

[root@kurol ~]# cat sed.rules s/this/THAT/g /^$/d [root@kurol ~]# sed -f sed.rules sed.txt #使用-f参数指定该脚本应用于sed.txt THAT is line 1, THAT is First line THAT is line 2, the Second line, Empty line followed THAT is line 4, THAT is Third line THAT is line 5, THAT is Fifth line
相关文章
相关标签/搜索