Linux之sed小练习
温馨提示:这篇文章已超过883天没有更新,请注意相关的内容是否还可用!
把/etc/passwd复制到/root/test.txt,用sed打印所有行
# /bin/cp /etc/passwd /root/test.txt && sed -n '1,$'p test.txt
打印test.txt,3到10行
# sed -n '3,10'p test.txt
打印test.txt包含root的行
# sed -n '/root/'p test.txt
删除test.txt的第15行及以后的所有行
# sed '15,$'d test.txt
删除test.txt中包含bash的行
# sed '/bash/'d test.txt
将test.txt中的root替换成toor
# sed 's/root/toor/'g test.txt
将test.txt中的/sbin/nologin替换为/bin/login
# sed 's#sbin/nologin#bin/login#g' test.txt
删除test.txt中的第五行-第十行中所有数字
# sed '5,10s/[0-9]//g' test.txt
删除test.txt中所有的特殊字符,(除了数字以及大小写字母)
# sed 's/[^0-9a-zA-Z]//g' test.txt
把test.txt中第一个单词和最后一个单词调换位置
# sed -r 's/(^[a-zA-Z]+)([^a-zA-Z].*[^a-zA-Z])([a-zA-Z]+$)/\3\2\1/' test.txt
把test.txt中出现的第一组数字(1个或者多个)和最后一个单词调换位置
# sed -r 's/(^[^0-9]*)([0-9]+)([^0-9].*[^a-zA-Z])([a-zA-Z]+$)/\1\4\3\2/' test.txt
把test.txt中第一个数字移动到本行末尾
# sed -r 's/(^[0-9]*)([0-9]+)([^0-9].*$)/\1\3\2/' test.txt
在test.txt第20行到最后一行最前面加aaa:。
# sed '20,$s/^.*$/aaa:&/' test.txt
若文章图片、下载链接等信息出错,请联系反馈,博主将第一时间更新!如果喜欢本站,请打赏支持本站,谢谢!
文章版权声明:除非注明,否则均为阿豪运维笔记原创文章,转载或复制请以超链接形式并注明出处。