您的位置:首页 > 其它

正则表达式之sed学习笔记

2015-12-04 14:24 260 查看
sed正则表达式学习笔记
#不能显示颜色。

-n //只打印匹配的行,不打印无关的行。

sed -n '10'p 1.txt //如果不加-n的话,会打印所有的行,到第10行时,会多打印一次。

sed -n '1,10'p 1.txt //打印第1到10行。

sed -n '30,$'p 1.txt

sed -n '/root/'p 1.txt //搜索含有root的行。

sed -n '/r.o/'p 1.txt //.表示任意一个字符,同grep.

sed -n '/r*o/'p 1.txt //只要有o就可以,*表示零到多个前面的字符。

sed -n '/r.*o/'p 1.txt //用.*表示任意字符。

sed -n '/r\?o/'p 1.txt //?表示零个或一个问号前面的字符。

#输入时不要用中文的标点符号。
sed -n '/r\+o/'p 1.txt //+表示一个或多个前面的字符。

sed -n '/root\|nologin/'p 1.txt //支持或者。

sed -n '/\(oo\)\+/'p 1.txt //一对或多对oo。

-r //类似于grep的E选项
sed -n -r '/(oo)+/'p 1.txt
sed -n -r '/root|nologin/'p 1.txt

sed -n '/[0-9]/'p 1.txt

sed -n '/^[0-9]/'p 1.txt

sed -n '/^[a-zA-Z]/'P 1.txt

sed -n '/^$/'p 1.txt //显示空行。

sed '/^$/'d 1.txt //删除空行。

sed '/[0-9]/'d 1.txt

sed '/[a-zA-Z]/'d 1.txt

sed '1,19'd 1.txt

-i //真正对文件内容执行修改。

sed -i '1,19'd 1.txt
替换:

sed '1,10s/nologin/login/g' 1.txt

sed '1,$s/\/sbin\/nologin/login/g' 1.txt

sed '1,$s#/sbin/nologin#login#g' 1.txt //可用#或@代替/,可以避免使用脱义符。

sed '1,$s@/sbin/nologin@login@g' 1.txt

sed 's@/sbin/nologin@login@g' 1.txt //全局替换。

sed 's@^.*$@login@g' 1.txt

sed 's@^.*$@&login@g' 1.txt //用&实现在所有行末尾加上login.

sed 's@^.*$@& login@g' 1.txt //用&实现在所有行末尾加上空格login

sed 's@[0-9]@@g' 1.txt //删除每一行的数字。

sed 's@[a-zA-Z]@@g' 1.txt

sed 's@[^0-9a-zA-Z]@@g' 1.txt

sed -r 's@^([a-z]+)(:.*:)(.*$)@\3\2\1@g' 1.txt //替换位置。

sed -n '/root/p ; /games/p' 1.txt //用分号隔开两条语句或表达式;另外如果某行显示两次,说明它同时具有root和games.表示两条命令。
#分号与|(或者)有本质区别
sed -n -r '/root|games/p' 1.txt

-e选项:

sed -n -e '/root/p' -e '/games/p' 1.txt //-e选项等同于用分号把两条语句隔开。

#sed -n '/root/p' /etc/passwd 跟sed -n '/root/'p /etc/passwd 格式都可以的吧。单引号里面的p或者g放在单引号外面也可以。

-n //不打印无关的行。
打印某行 :

sed -n '2'p test001.txt //打印出第2行。

sed -n '1,$'p test001.txt //打印出所有的行。

sed -n '1,3'p test001.txt //打印出第1-3行。

打印包含某个字符串的行 :

sed -n '/root/'p test001.txt //打印包含字符串root的行。

grep中使用的特殊字符,如 ^ $ . * 等同样也能在sed中使用:

sed -n '/^r/'p test001.txt //打印以r开头的行。

sed -n '/sh$/'p test001.txt //打印以sh结尾的行。

sed -n '/r..t/'p test001.txt //打印r与t之间包含任意两个字符的行。

sed -n '/aaa*/'p test001.txt //打印aaa后跟任意字符的行。

-e //可以实现多个行为 。

sed -e '/root/'p test001.txt -e '/spool/'p test002.txt

删除某行或者多行 :

sed '/aaa/‘d test001.txt //删除包含字符串aaa行,并打印出余下的行。(并非真正删除)

sed '1'd test001.txt //删除第一行,并打印出余下的行。

sed '1,3'd test001.txt //删除第1到3行,并打印出余下的行。

sed '1,$'d test001.txt //删除所有行。

替换字符或字符串 :

sed '1,2s/root/toor/g' test001.txt
//上例中的 ‘s’ 就是替换的命令, ‘g’ 为本行中全局替换,如果不加 ‘g’ 只换该行中出现的第一个。
除了可以使用 ‘/’ 作为分隔符外,还可以使用其他特殊字符例如 ‘#’ 或者 ‘@’ 都没有问题

删除文档中的所有数字或者字母:

sed '1,$s/[0-9]//g' test001.txt

sed '1,$s/[a-zA-Z]//g' test001.txt

调换两个字符串的位置 :

sed 's/\(root\)\(.*\)\(bash\)/\3\2\1/g' test001.txt //首尾两个单词调换并打印。

直接修改文件的内容 :

-i //直接在未见内容修改的选项。

sed -i 's/ro/oR/g' test001.txt

习题:

把/etc/passwd 复制到/root/test.txt,用sed打印所有行
1.打印test.txt的3到10行
sed -n '3,10'p test001.txt
打印test.txt 中包含 ‘root’ 的行
sed -n '/root/'p test001.txt
删除test.txt 的15行以及以后所有行
sed -n '15,$'p test001.txt
删除test.txt中包含 ‘bash’ 的行
sed -n '/bash/'p test001.txt
替换test.txt 中 ‘root’ 为 ‘toor’
sed 's/root/toor/g' test001.txt
替换test.txt中 ‘/sbin/nologin’ 为 ‘/bin/login’
sed 's@/sbin/nologin@/bin/login@g' test001.txt
删除test.txt中5到10行中所有的数字
sed '5,10s/[0-9]//g' test001.txt
删除test.txt 中所有特殊字符(除了数字以及大小写字母)
sed '1,$s/[^0-9a-zA-Z]//g' test001.txt
把test.txt中第一个单词和最后一个单词调换位置
sed -r 's/(^[a-z]+)(:.*:)(.*$)/\3\2\1/' test001.txt
把test.txt中出现的第一个数字和最后一个单词替换位置
sed -r 's/0(:.*:)(.*$)/\3\2\1/' test001.txt
把test.txt 中第一个数字移动到行末尾

在test.txt 20行到末行最前面加 ‘aaa:’
sed '20,$s/^.*$/aaa&/g' test001.txt

本文出自 “sunwangbackup” 博客,请务必保留此出处http://sunwangbackup.blog.51cto.com/10866322/1719542
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: