sed
1、sed的语法格式:
sed在正常情况下,将处理的行读入模式空间(pattern space),脚本中的“sed-command(sed命 令)”就一条接着一条进行处理,知道脚本执行完毕。然后该行呗输出,模式(pattern space)被清 空;接着,在重复执行刚才的动作,文件中的新的一行被读入,直到文件处理完毕。 但是,由于种种原因,如用户希望在某个条件下,脚本中的某个命令被执行或希望模式空间(pattern space)保留,以便下一次使用,这都有可能使sed在处理文件的时候,不按照正常的流程来进行处 理,这时候就需要用sed高级命令来满足需求。
2、sed选项 option
-n 使用安静模式.则只有经过sed特殊处理的哪一行才会被列出来
-e 传送带(前面执行完传递给后面)
-f 跟随脚本文件名
-r 脱意
-i 直接修改读取文件
3、sed命令详解 command
a 新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c 取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d 删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i 插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p 列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
s 取代,通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g
n 读取下一个输入行, 用下一个命令处理新的行
r 从另一个文件读
4、以下是替换标记
g 表示行内全面替换
p 表示打印行
w 表示把行写入一个文件
x 表示互换模快板中的文本和缓冲区中的文本
y 表示把一个字符翻译为另外的字符(不用于正则表达式)
5、元字符集
,匹配符 ^ 锚定行的开始. 如/^sed/ 匹配所有以sed开头的行
$ 锚定行的结束. 如/sed$/ 匹配所有以sed结尾的行
. 匹配一个非换行符. 如/sed/ 匹配s后接一个任意字符然后是d
* 匹配零或多个字符. 如/sed/ 匹配所有模块是一个或多个空格后紧跟sed的行
[] 匹配一个制定范围内的字符如. /[Ss]ed/ 匹配sed和Sed
[^] 匹配一个不在制定范围内的字符. 如/[^A-RT-Z]ed/ 匹配不包含A-R和T-Z的一个字母开头,紧跟ed的行
(..) 保存匹配的字符. 如s/(love)able/\1rs loveable被替换成lovers
& 保存搜索字符用来替换其它字符. 如s/love/&/ love这成love
< 锚定单词的开始. 如/\> 锚定单词的结束. 如/love>匹配包含以love结尾的单词行
x{m} 重复字符x,m次. 如/o{5}匹配包含5个o的行 x{m,} 重复字符x,至少m次. 如 /o{5,}/匹配至少有5个o的行
x{m,n} 重复字符x,至少m次.把多余n次. 如 /o{5,10}/匹配5-10个o的行
6、sed中查找模式匹配
. 单字通配符
[0-9] 匹配0-9
[^a-z] 不匹配a-z的所有其它字符
“.*” 匹配””内任何字符串
^ 匹配行开始
$ 匹配行结束
7、例子
删除: dd命令
sed ‘2d’ file 删除file文件的第2行
sed ‘2,$d’ file 删除file文件的第2行到末尾所有行
sed ‘$d’ file 删除file文件的最后一行
sed ‘/test/’d file 删除file文件所有包含test的行
sed d file 删除file文件中的所有行
替换: s命令
sed ‘s/test/mytest/g’ file 在整行范围内把test替换为mytest. 如果没有g标记,则只有每行第一个匹配 的test被替换成mytest
sed -n ‘s/^test/mytest/p’ file -n选项和p标志一起使用表示只打印那些发生替换的行, 如果某一行开头 的test被替换成mytest,就打印它
sed ‘s/^192.168.1.1/&localhost’ file &符号表示替换字符串中被找到的部分. 所有以192.168.1.1开头 的行都会被替换成它自己加localhost,变成192.168.1.1localhost sed -n ‘s/(love)able/\1rs/p’ file love被标记为1,所有loveable会被替换成lovers,而且替换的行会被打印 出来
sed ‘s#10#100#g’ file 不论什么字符,紧跟着s的命令都被认为是最新的分隔符,所以,’#’在这里是分隔 符,代替了默认的”/”分隔符. 表示把所有10替换成100
选定行的范围: 逗号
sed -n ‘/test/,/check/p’ file 所有在模块test和check所确定范围内的行都被打印
sed -n ‘5,/^test/p’ file 打印从第5行开始到第一个包含以test开始行之间的所有行
sed ‘/test/,/west/s/$/sed test/’ file 对于模块test和west之间的行,每行的末尾用字符串sed test替换
多点编辑: e命令
sed -e ‘1,5d’ -e ‘s/test/check/’ file -e选项允许在同一行里执行多条命令.删除1-5行,check替换test
sed -e ‘/and/s/aaa/bbb’ file sed中用bbb替换同一行中包括字符串and的字符串aaa,而不是每一行中 的字符串aaa 读入文件: r命令
sed ‘/test/r file_a’ file file_a里的内容被读进来,显示在与test匹配的行后面. 如果匹配多行,则file_a内 容将显示在所有匹配行的下面
写入文件: w命令
sed -n ‘test/w file_a’ file file中所包含的test行都被写入file里
插入: i命令
sed ‘/test/i\ new line’ file 如果test被匹配, 则把反斜杠后面的文本插入到匹配行的上面
sed -e ‘1 ia’ -e ‘$ a3′ file 在第一行上面插入a,在末尾行下面插入3
下一个: n命令
sed高级用法:
N命令
[root@cb ~]# cat test.txt Make sure that the heap size is set to about half the memory available on the system and that the owner of the process is allowed to use this limit [root@cb ~]# sed '/lable/{N;s/\n/ /}' test.txt Make sure that the heap size is set to about half the memory available on the system and that the owner of the process is allowed to use this limit
D命令
[root@cb ~]# cat test1 This is followed 1 This is followed 2 This is followed 3 This is followed 4 This is end [root@cb ~]# sed '/^$/{N;/^\n$/d}' test1 This is followed 1 This is followed 2 This is followed 3 This is followed 4 This is end [root@cb ~]# sed '/^$/{N;/^\n$/D}' test1 This is followed 1 This is followed 2 This is followed 3 This is followed 4 This is end
sed匹配UNIX$结尾的行,用N将匹配到的行和下一行加入到模式空间,然后再匹配\nSystem,在里面新增内容System的上一行结尾新增内容,删除模式空间中Here are examples of the UNIX Operating,在次执行前面的内容,在下一个匹配到的\nSystem的上一行结尾增加内容,删除模式空间第二次匹配到的System. Where UNIX Operating,最结果就是上面。
保持空间最常的用途是,当改变模式空间中的原始内容时,用于保留当前输入行的副本。影响模式空间的命令有:
| 命令 | 缩写 | 功能 |
|---|---|---|
| Hold | h或H | 将模式空间的内容复制或追加到保持空间 |
| Get | g或G | 将保持空间的内容复制或追加到模式空间 |
| Exchange | x | 交换保持空间或模式空间的内容 |
&的用法
[root@cb ~]# cat adc1 Alice Ford, 22 East Broadway, Richmond VA [root@cb ~]# sed 's/22/&3/' abc1 Alice Ford, 223 East Broadway, Richmond VA [root@cb ~]# sed 's/22/& 3/' abc1 Alice Ford, 22 3 East Broadway, Richmond VA [root@cb ~]# sed 's/22/3&/' abc1 Alice Ford, 322 East Broadway, Richmond VA [root@cb ~]# sed -r 's/[0-9]{2}/&3/' abc1 Alice Ford, 223 East Broadway, Richmond VA
浙公网安备 33010602011771号