透彻分析sed命令
基本作用
sed命令可以通过一个(或多个)表达式或者脚本来编辑和处理(多个)文件,简化对文件的反复操作、编写转换程序等。
命令基础格式
sed [-hnV][-e<script>][-f<script文件>][文本文件]
基本参数
sed --v #查看版本 =--version
sed --h #查看帮助 = --help
sed -n #静默执行 = --quiet --silent
sed -e #执行表达式script, --expression=script 不改变源文件内容
sed -f #执行脚本script-file = --file = script-file
sed -i #直接修改源文件
sed -l 指定“l”命令的换行长度
sed -r 使用扩展正则表达式
操作参数
a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!
扩展正则表达式
^ 匹配行开始,如:/^sed/匹配所有以sed开头的行。
$ 匹配行结束,如:/sed$/匹配所有以sed结尾的行。
^$ 空白行
. 匹配任意单个非换行符字符
* 匹配相邻的字符任意次(0或多个)
.* 匹配任意长度的任意字符
\? 匹配紧挨在前面的字符0次或1次
x{m} 匹配重复字符x,m次,如:/0{5}/匹配包含5个0的行
x{m,} 匹配重复字符x,至少m次,如:/0{5,}/匹配至少有5个0的行
x{m,n} 匹配重复字符x,至少m次,不多于n次,如:/0{5,10}/匹配5~10个0的行
< 匹配单词的开始,如:/<love/匹配包含以love开头的单词的行
> 匹配单词的结束,如/love>/匹配包含以love结尾的单词的行
<pattern> 匹配单词
[] 匹配指定范围内的任意单个字符,如/[sS]ed/匹配sed和Sed
[^] 匹配不在指定范围内的任意单个字符
() 匹配子串,保存匹配的字符,如s/(love)able/\1rs,loveable被替换成lovers。
[:digit:] 所有数字, 相当于0-9, [0-9]= [[:digit:]]
[:lower:] 所有的小写字母
[:upper:] 所有的大写字母
[:alpha:] 所有的字母
[:alnum:] 相当于0-9a-zA-Z
[:space:] 空白字符
[:punct:] 所有标点符号
s替换标记[flags]
g替换全部
number只替换number行
p如果替换则打印
w将替换的结果写入文件
\l子串匹配
命令举例
以 1.txt文件为例,文件中原始内容如下:
jianglei@ubuntu:~$ cat 1.txt
this is the 1 line.
this is the 2 line.
this is the 3 line.
this is the 4 line.
this is the 5 line.
this is the 6 line.
this is the 7 line.
this is the 8 line.
this is the 9 line.
this is the 10 line.
#在第四行后插入一行 ,打印输出,不改变源文件 -e不改变源文件, -i直接修改源文件,操作系统文件时慎重
sed -e '4a\this is a insert line' 1.txt
#在第四行后插入一行 ,打印输出,改变源文件
sed -i '4a\this is a insert line' 1.txt
#在第四行后插入一行 ,打印输出,不改变源文件,重定向到文件
sed -e '4a\this is a insert line' 1.txt >new_1.txt
#每行后面都增加一个空白行
sed -e 'a\\n' 1.txt
#以this开头的行换行增加一个注释。
sed '/^this/a\\--->this is a example' 1.txt
#删除第五行
sed -e '5d' 1.txt
#删除前五行
sed -e '1,5d' 1.txt
#删除5行以后的数据
sed -e '5,$d' 1.txt
#删除2-5行,nl =cat -n
nl ./1.txt | sed '2,5d'
#每行前面插入一个test
nl 1.txt | sed -e 'i test'
#第二行前面插入一个test
nl 1.txt | sed -e '2i test'
#第二行前面插入多行数据 注意首行与最后一行包含引号,行与行之间包含\
nl 1.txt | sed -e '2i test\
> sfdsf\
> dsfdfd\
> dfdfd'
#只显示567行
sed -n '5,7p' 1.txt
nl 1.txt |sed -n '5,7p'
# 显示包含数字9的行
nl 1.txt | sed -n '/9/p'
#显示包含有数字0-7的行
nl 1.txt | sed -n '/[0-7]/p'
#删除所有包含数字的行
nl 1.txt | sed -n '/[0-9]/d'
#找到包含this的行,并把this改为that
sed -e '/this/{s/this/that/g}' 1.txt
#提取IP地址:
/sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
#多点编辑:一条sed命令,删除/etc/passwd第三行到末尾的数据,并把bash替换为blueshell
nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/'
#将1.txt中的2-5行删除,将this替换为that,并在处理后将第二行替换为this is a newline
nl 1.txt | sed '2,5d'| sed 's/this/that/g'|sed '2c this is a newline'
#将IP追加重定向到文件
/sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' >> ./sed.txt
#打印前5行
sed -n '1,5p' sed.txt
sed '5q' sed.txt #必须从0开始
字符串替换:
sed '/today/,/hello/s/$/www/' sed.txt 对于模板today和hello之间的行,每行的末尾用字符串www替换
sed '/today/,/hello/s/^/www/' sed.txt 对于模板today和hello之间的行,每行的开头用字符串www替换
sed '/^[A-Za-z]/s/5/five/g' sed.txt 将以字母开头的行中的数字5替换成five
sed 's/hello/hi/g' sed.txt 在整行范围内把hello替换为hi。如果没有g标记,则只有每行第一个匹配的hello被替换成hi。
sed 's/hello/hi/2' sed.txt 只替换每行的第2个hello为hi
sed 's/hello/hi/2g' sed.txt 只替换每行的第2个以后的hello为hi(包括第2个)
sed -n 's/^hello/hi/p' sed.txt -n和p标志一起使用表示只打印那些发生替换的行。也就是说,如果某一行开头的hello被替换成hi,就打印它
sed -n '2,4p' sed.txt 打印输出sed.txt中的第2行和第4行
sed -n 's/hello/&-hi/gp' sed.txt &符号表示追加一个串到找到的串后
sed 's/^192.168.0.1/&-localhost/' sed.txt 所有以192.168.0.1开头的行都会被替换成它自已加 -localhost,变成192.168.0.1-localhost
sed 's/^192.168.0.1/[&]/' sed.txt 表示给IP地址添加中括号
sed 's#hello#hi#g' sed.txt 把所有hello替换成hi不论什么字符,紧跟着s命令的都被认为是新的分隔符,所以,"#"在这里是分隔符,代替了默认的"/"分隔符
sed -n '/today/,/hello/p' sed.txt 所有在第一个today和第一个hello所确定的范围内的行都被打印
sed -n '5,/^hello/p' sed.txt 打印从第五行开始到第一个包含以hello开始的行之间的所有行
sed -n '/^hello/,8p' sed.txt 打印从第一个包含以hello开始的行到第8行之间的所有行
sed "s/$//;s/ *//g;/^$/d"
sed -e s/$// sed.txt $是最后。在每一行后面追加空。 s为搜索,如s/a/b/,搜索a将替换为b,并只替换一次。
s/ *//g 将空格删除。 g代表搜索到的全部替换。"空格*"即" *"代表多个空格。
/^$/d 删除空行。
#在每一行最后追加一个字符串,也可换行
sed -e s/$/last/ sed.txt
#在每一行最后追加一个换行
sed -e s/$/\\n/ sed.txt
#删除所有空格
sed -e s/\\s//g sed.txt
#删除所有空行
sed -e /^$/d sed.txt