shell之sed

7年前 (2017-07-26) gtj linux, shell 0评论 已收录 679℃

sed命令的选项(option):

先读取资料、存入模式空间、对其进行编辑、再输出、再用下一行替换模式空间内容
-n # 输出由编辑指令控制(取消默认的输出,必须与编辑指令一起配合)
-i # 直接对文件操作
-e # 多重编辑
-r # 正则可不转移特殊字符
b # 跳过匹配的行
p # 打印
d # 删除
s # 替换
g # 配合s全部替换
i # 行前插入
a # 行后插入
r # 读
y # 转换
q # 退出
& # 代表查找的串内容
* # 任意多个 前驱字符(前导符)
? # 0或1个 最小匹配 没加-r参数需转义 \?
$ # 最后一行
.* # 匹配任意多个字符
\(a\) # 保存a作为标签1(\1)

打印某一行:
sed '2p' sed-test.txt ##打印所有行,匹配到的行打印两遍

[root@oldboy ~]# sed '2p' sed-test.txt 
thisis first
this 2
this 2
this 3
htis 4
this 5
this 6

[root@oldboy ~]# sed  -n '2p' sed-test.txt  ##只打印匹配的行
this 2
[root@oldboy ~]# 

[root@oldboy ~]# sed -n '1,3p' sed-test.txt  ##打印1-3行
thisis first
this 2
this 3
[root@oldboy ~]# 

[root@oldboy ~]# sed -n '/first/p' sed-test.txt  ##打印含有特定字符的行
thisis first
[root@oldboy ~]#  

<h3>[root@oldboy ~]#  sed -n '/htis/,/4/p' sed-test.txt  ##打印含有两个特定字符串的行</h3>
htis 4
htis 4
[root@oldboy ~]# 


[root@oldboy ~]# sed -n '/first/,/4/p' sed-test.txt  ##打印配到的行到第四行
thisis first
this 2
this 3
htis 4 


[root@oldboy ~]# sed -n '/2/,5p' sed-test.txt ##打印配到的行到第五行
this 2
this 3
htis 4
htis 4

[root@oldboy ~]# sed -n '1,4{=;p}' sed-test.txt  #打印1-4行且打印行号
1
thisis first
2
this 2
3
this 3
4
htis 4

sed -n '/o\{1,3\}/p'/etc/passwd ##打印o出现1到三次的
sed -n '/^#/!p' /etc/passwd ##打印开始不带#号的行
sed -e '/^#/d' -e '/^$/d' /etc/host.conf  ##删除以#开始的行并且删除空行。
[root@oldboy ~]# sed '/first/s/^/# /' sed-test.txt  ##匹配的行行首加#号
# thisis first
this 2
this 3
htis 4
htis 4
this 5
this 5
this 6
this 6

[root@oldboy ~]# sed 's/first/test &/' sed-test.txt ## 匹配的字符串的前面加test
thisis test first
this 2
this 3
htis 4
htis 4
this 5
this 5
this 6
this 6
[root@oldboy ~]# sed 's/first/&  test/' sed-test.txt  ##匹配的行后加test
thisis first  test
this 2
this 3
htis 4
htis 4
this 5
this 5
this 6
this 6
[root@oldboy ~]# sed '/first/s/$/   #/' sed-test.txt  ##匹配的行后加#
thisis first   #
this 2
this 3
htis 4
htis 4
this 5
this 5
this 6
this 6
[root@oldboy ~]# sed  -n '/3\{1,\}/p' sed-test.txt  ##匹配3出现1次以上的行
this 3  3  3 3  3 3 3 3
##匹配字符串中含有4个以上个5的行:
[root@oldboy ~]# sed -n '/5\{4,\}/p' sed-test.txt 
this 55555
##匹配字符串中含有4到7个5的行
[root@oldboy ~]# sed -n '/5\{4,7\}/p' sed-test.txt 
this 55555
[root@oldboy ~]# sed -n '/5\{6,7\}/p' sed-test.txt 
[root@oldboy ~]# sed -n '/5\{5,7\}/p' sed-test.txt 
this 55555
a: 行后加一行
i: 行前加一行
[root@oldboy ~]# sed '2ahello' sed-test.txt ##匹配行的行后加一行
this is first
this 2  2 
hello
this 3  3  3 3  3 3 3 3
htis 4   4 4
htis 4
this 55555
this 5
this 6
this 6
[root@oldboy ~]# sed '/2/c\thhh' sed-test.txt ##匹配到2的行换成thhh
this is first
thhh
this 3  3  3 3  3 3 3 3
htis 4   4 4
htis 4
this 55555
this 5   this 
this 6
this 6
[root@oldboy ~]# sed '2ihello' sed-test.txt ##匹配行的行前加一行
this is first
hello
this 2  2 
this 3  3  3 3  3 3 3 3
htis 4   4 4
htis 4
this 55555
this 5
this 6
this 6

[root@oldboy ~]# sed 's/this/that/'  sed-test.txt  ##匹配到的字符串替换成指定的字符串
that is first
that 2  2 
that 3  3  3 3  3 3 3 3
htis 4   4 4
htis 4
that 55555
that 5   this 
that 6
that 6

[root@oldboy ~]# sed 's/this/that/g'  sed-test.txt  ##全文匹配到的字符串替换成指定的字符串
that is first
that 2  2 
that 3  3  3 3  3 3 3 3
htis 4   4 4
htis 4
that 55555
that 5   that 
that 6
that 6

[root@oldboy ~]# sed -n '/2/p' sed-test.txt ##打印匹配的行
this 2  2 
[root@oldboy ~]# sed '/this/!d' sed-test.txt  ##删除不包含this的行
this is first
this 2  2 
this 3  3  3 3  3 3 3 3
this 55555
this 5   this 
this 6
this 6
[root@oldboy ~]# sed '/2/{s/this/that/}' sed-test.txt  ##匹配含有2的行 把其中的this替换成that
this is first
that 2  2 
this 3  3  3 3  3 3 3 3
htis 4   4 4
htis 4
this 55555
this 5   this 
this 6
this 6
[root@oldboy ~]# sed '$d' sed-test.txt  ##删除最后一行
this is first
this 2  2 
this 3  3  3 3  3 3 3 3
htis 4   4 4
htis 4
this 55555
this 5   this 
this 6
[root@oldboy ~]# sed -n '2,$p' sed-test.txt ##打印2到最后一行
this 2  2 
this 3  3  3 3  3 3 3 3
htis 4   4 4
htis 4
this 55555
this 5   this 
this 6
this 6
sed '/^\//d' file ##删除行首的"/"
sed 's#\{1,\}/ /' #将一个以上的#换成空格


[root@oldboy ~]# sed '1,4 s/this/that/'  sed-test.txt  ##将第一行到第四行的this替换成that
that is first
that 2  2 
that 3  3  3 3  3 3 3 3
htis 4   4 4
htis 4
this 55555
this 5   this 
this 6
this 6
[root@oldboy ~]# sed 's/3/44/2g' sed-test.txt ##将匹配行中从第二次出现的3替换到最后
this is first
this 2  2 
this 3  44  44 44  44 44 44 44
htis 4   4 4
htis 4
this 55555
this 5   this 
this 6
this 6
[root@oldboy ~]# sed 's/3/44/2' sed-test.txt  ##将匹配行中第二次出现的3替换成44
this is first
this 2  2 
this 3  44  3 3  3 3 3 3
htis 4   4 4
htis 4
this 55555
this 5   this 
this 6
this 6
[root@oldboy ~]# sed '/^htis/d' sed-test.txt  ##删除以htis开始的行
this is first
this 2  2 
this 3  3  3 3  3 3 3 3
this 55555
this 5   this 
this 6
this 6
[root@oldboy ~]# sed '/^htis/!d' sed-test.txt 
htis 4   4 4
htis 4
[root@oldboy ~]# 
 sed -i '/root/{s/bash/nologin/;s/0/1/g}' test  
        #匹配root的行,把bash替换成nologin,且把0替换成1  

sed -i 's/ONBOOT/#&/' file    or  sed -i  '/'$IP3',/server/s/^/#/' file               
        #匹配ONBOOT的行的前面添加#号,在配置文件中也表示注释某行  
sed -i '/ONBOOT/s/#//' test                  
        #匹配ONBOOT的行,把#替换成空,即去掉#号,也一般用作去掉#注释
[root@oldboy ~]# sed -i 's/htis/w sed.txt' sed-test.txt 
sed:-e 表达式 #1,字符 16:未终止的“s”命令
[root@oldboy ~]# sed -i '/htis/w sed.txt' sed-test.txt  ##把匹配到的行保存到另一个文件中

[root@oldboy ~]# cat sed.txt 
htis 4   4 4
htis 4

生产实践

1)、处理以下文件内容,将域名取出并进行计数排序,如处理:
http://www.baidu.com/1.html
http://post.baidu.com/index.html
http://mp3.baidu.com/index.html
http://www.baidu.com/3.html
http://post.baidu.com/2.html
得到如下结果:
域名的出现的次数 域名
2 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com
cat file | sed -e ' s/http:\/\///' -e ' s/\/.*//' | sort | uniq -c | sort -rn
2 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com
awk -F / '{print $3}' file |sort -r|uniq -c|awk '{print $1"\t",$2}'
2 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com
2)、用grep结合sed取出网卡的ip地址
ifconfig | grep -B1 "inet addr" |grep -v "\-\-" |sed -n -e 'N;s/eth[0−9].*\n.*addr:[0−9]{1,3}\.[0−9]{1,3}\.[0−9]{1,3}\.[0−9]{1,3}.*/\1 \2/p'

以下内容来自网络整理:

sed 10q # 显示文件中的前10行 (模拟"head")
sed -n '$=' # 计算行数(模拟 "wc -l")
sed -n '5,/^no/p' # 打印从第5行到以no开头行之间的所有行
sed -i "/^$f/d" a     # 删除匹配行
sed -i '/aaa/,$d' # 删除匹配行到末尾
sed -i "s/=/:/" c # 直接对文本替换
sed -i "/^pearls/s/$/j/" # 找到pearls开头在行尾加j
sed '/1/,/3/p' file # 打印1和3之间的行
sed -n '1p' 文件 # 取出指定行
sed '5i\aaa' file # 在第5行之前插入行
sed '5a\aaa' file # 在第5行之后抽入行
echo a|sed -e '/a/i\b' # 在匹配行前插入一行
echo a|sed -e '/a/a\b' # 在匹配行后插入一行
echo a|sed 's/a/&\nb/g' # 在匹配行后插入一行
seq 10| sed -e{1,3}'s/./a/' # 匹配1和3行替换
sed -n '/regexp/!p' # 只显示不匹配正则表达式的行
sed '/regexp/d' # 只显示不匹配正则表达式的行
sed '$!N;s/\n//' # 将每两行连接成一行
sed '/baz/s/foo/bar/g' # 只在行中出现字串"baz"的情况下将"foo"替换成"bar"
sed '/baz/!s/foo/bar/g' # 将"foo"替换成"bar",并且只在行中未出现字串"baz"的情况下替换
echo a|sed -e 's/a/#&/g' # 在a前面加#号
sed 's/foo/bar/4' # 只替换每一行中的第四个字串
sed 's/\(.*\)foo/\1bar/' # 替换每行最后一个字符串
sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # 替换倒数第二个字符串
sed 's/[0-9][0-9]$/&5' # 在以[0-9][0-9]结尾的行后加5
sed -n ' /^eth\|em[01][^:]/{n;p;}' # 匹配多个关键字
sed -n -r ' /eth|em[01][^:]/{n;p;}' # 匹配多个关键字
echo -e "1\n2"|xargs -i -t sed 's/^/1/' {} # 同时处理多个文件
sed '/west/,/east/s/$/*VACA*/' # 修改west和east之间的所有行,在结尾处加*VACA*
sed 's/[^1-9]*\([0-9]\+\).*/\1/' # 取出第一组数字,并且忽略掉开头的0
sed -n '/regexp/{g;1!p;};h' # 查找字符串并将匹配行的上一行显示出来,但并不显示匹配行
sed -n ' /regexp/{n;p;}' # 查找字符串并将匹配行的下一行显示出来,但并不显示匹配行
sed -n 's/\(mar\)got/\1ianne/p' # 保存\(mar\)作为标签1
sed -n 's/\([0-9]\+\).*\(t\)/\2\1/p' # 保存多个标签
sed -i -e '1,3d' -e 's/1/2/' # 多重编辑(先删除1-3行,在将1替换成2)
sed -e 's/@.*//g' -e '/^$/d' # 删除掉@后面所有字符,和空行
sed -n -e "{s/文本(正则)/替换的内容/p}" # 替换并打印出替换行
sed -n -e "{s/^ *[0-9]*//p}" # 打印并删除正则表达式的那部分内容
echo abcd|sed 'y/bd/BE/' # 匹配字符替换
sed '/^#/b;y/y/P/' 2 # 非#号开头的行替换字符
sed '/suan/r 读入文件' # 找到含suan的行,在后面加上读入的文件内容
sed -n '/no/w 写入文件' # 找到含no的行,写入到指定文件中
sed '/regex/G' # 在匹配式样行之后插入一空行
sed '/regex/{x;p;x;G;}' # 在匹配式样行之前和之后各插入一空行
sed 'n;d' # 删除所有偶数行
sed 'G;G' # 在每一行后面增加两空行
sed '/^$/d;G' # 在输出的文本中每一行后面将有且只有一空行
sed 'n;n;n;n;G;' # 在每5行后增加一空白行
sed -n '5~5p' # 只打印行号为5的倍数
seq 1 30|sed '5~5s/.*/a/' # 倍数行执行替换
sed -n '3,${p;n;n;n;n;n;n;}' # 从第3行开始,每7行显示一次
sed -n 'h;n;G;p' # 奇偶调换
seq 1 10|sed '1!G;h;$!d' # 倒叙排列
ls -l|sed -n '/^.rwx.*/p' # 查找属主权限为7的文件
sed = filename | sed 'N;s/\n/\t/' # 为文件中的每一行进行编号(简单的左对齐方式)
sed 's/^[ \t]*//' # 将每一行前导的"空白字符"(空格,制表符)删除,使之左对齐
sed 's/^[ \t]*//;s/[ \t]*$//' # 将每一行中的前导和拖尾的空白字符删除
sed '/{abc,def\}\/\[111,222]/s/^/00000/' # 匹配需要转行的字符: } / [
echo abcd\\nabcde |sed 's/\\n/@/g' |tr '@' '\n' # 将换行符转换为换行
cat tmp|awk '{print $1}'|sort -n|sed -n '$p' # 取一列最大值
sed -n '{s/^[^\/]*//;s/\:.*//;p}' /etc/passwd # 取用户家目录(匹配不为/的字符和匹配:到结尾的字符全部删除)
sed = filename | sed 'N;s/^/ /; s/ *\(.\{6,\}\)\n/\1 /' # 对文件中的所有行编号(行号在左,文字右端对齐)
/sbin/ifconfig |sed 's/.*inet addr:\(.*\) Bca.*/\1/g' |sed -n '/eth/{n;p}' # 取所有IP
博主

这货来去如风,什么鬼都没留下!!!

相关推荐

嗨、骚年、快来消灭0回复。

×
订阅图标按钮
Less is more!!!