shell脚本学习(三)- sed
2016-12-09 10:46
288 查看
1. 打印指定行
sed ‘10’p –n 1.txt; sed‘1,4’p –n 1.txt; sed ‘5,$’p –n 1.txt;
2. 打印包含某个行字符串的行
sed –n ‘/root/’p 1.txt 可以使用^
. * $等特殊符号
3. –e可以实现多个任务同时打印 sed
–e ‘/root’p –e ‘/body/’p –n 1.txt
4. 删除行
sed ‘/root/’d
1.txt ; sed ‘1d’ 1.txt; sed ‘1,10d’1.txt
5. 替换
sed ‘1,2s/ot/to/g’1.txt,其中s就是替换的意思,g为全局替换
6. 删除所有数字
sed ‘s/[0-9]//g’ 1.txt
7. 删除所有非数字
sed ‘s/^[0-9]//g’ 1.txt
8. 直接修改文件内容 sed
–i ‘s/ot/to/g’ 1.txt
9. 调换两个字符串位置
head –n2 1.txt | sed‘s/\(root\)\(.*\)/\(bash\)/3/2/1/’
10. 把/etc/passwd复制到/root/test.txt,用sed打印所有行
cp /etc/passwd/root/test.txt && sed ‘1,$’p /root/test.txt
11. 打印test.txt的3到10行
sed
–n‘3,10’p test.txt
12. 打印test.txt中包含root的行
sed –n ‘/root/’ptest.txt
13. 删除test.txt的15行以及以后的所有行
nl test.txt| sed '15,$'d
14. 删除包含bash的行
nl test.txt | sed '/bash/'d
15. 替换test.txt中root为bash
nl test.txt | sed ‘s/root/bash/g’
16. 替换test.txt中/sbin/nologin为/bin/login
nl test.txt | sed 's/\/sbin\/nologin/\/bin\/login/g'
17. 删除test.txt中5到10行的数字
nl test.txt | sed'5,10s/[0-9]//g'
18. 删除test.txt中的特殊字符除过数字字母 nl
test.txt |sed 's/[^0-9a-zA-Z]//g'
19. 把test.txt中第一个单词和最后一个单词调换位置
(暂时不会)
20. 把test.txt中出现的第一个数字和最后一个单词替换位置
(暂时不会)
21. 把test.txt中第一个数字移动到行末尾
(暂时不会)
22. 把test.txt20行到末尾行前面加aaa
(暂时不会)
23. 显示一下行号
nl passwd | sed ‘/root/’p –n
24. 对于sed的?+就需要脱义符号,否则需要-r参数
25. 这个有点高端了
headpasswd | sed -r 's/([^:]+)(:.*:)([^:]+$)/\3\2\1/'
sed ‘10’p –n 1.txt; sed‘1,4’p –n 1.txt; sed ‘5,$’p –n 1.txt;
2. 打印包含某个行字符串的行
sed –n ‘/root/’p 1.txt 可以使用^
. * $等特殊符号
3. –e可以实现多个任务同时打印 sed
–e ‘/root’p –e ‘/body/’p –n 1.txt
4. 删除行
sed ‘/root/’d
1.txt ; sed ‘1d’ 1.txt; sed ‘1,10d’1.txt
5. 替换
sed ‘1,2s/ot/to/g’1.txt,其中s就是替换的意思,g为全局替换
6. 删除所有数字
sed ‘s/[0-9]//g’ 1.txt
7. 删除所有非数字
sed ‘s/^[0-9]//g’ 1.txt
8. 直接修改文件内容 sed
–i ‘s/ot/to/g’ 1.txt
9. 调换两个字符串位置
head –n2 1.txt | sed‘s/\(root\)\(.*\)/\(bash\)/3/2/1/’
10. 把/etc/passwd复制到/root/test.txt,用sed打印所有行
cp /etc/passwd/root/test.txt && sed ‘1,$’p /root/test.txt
11. 打印test.txt的3到10行
sed
–n‘3,10’p test.txt
12. 打印test.txt中包含root的行
sed –n ‘/root/’ptest.txt
13. 删除test.txt的15行以及以后的所有行
nl test.txt| sed '15,$'d
14. 删除包含bash的行
nl test.txt | sed '/bash/'d
15. 替换test.txt中root为bash
nl test.txt | sed ‘s/root/bash/g’
16. 替换test.txt中/sbin/nologin为/bin/login
nl test.txt | sed 's/\/sbin\/nologin/\/bin\/login/g'
17. 删除test.txt中5到10行的数字
nl test.txt | sed'5,10s/[0-9]//g'
18. 删除test.txt中的特殊字符除过数字字母 nl
test.txt |sed 's/[^0-9a-zA-Z]//g'
19. 把test.txt中第一个单词和最后一个单词调换位置
(暂时不会)
20. 把test.txt中出现的第一个数字和最后一个单词替换位置
(暂时不会)
21. 把test.txt中第一个数字移动到行末尾
(暂时不会)
22. 把test.txt20行到末尾行前面加aaa
(暂时不会)
23. 显示一下行号
nl passwd | sed ‘/root/’p –n
24. 对于sed的?+就需要脱义符号,否则需要-r参数
25. 这个有点高端了
headpasswd | sed -r 's/([^:]+)(:.*:)([^:]+$)/\3\2\1/'
相关文章推荐
- shell脚本学习:不得不细心之sed和grep,sort和uniq,从细节看“认真”
- shell脚本学习:正则表达式之sed,tr篇
- shell脚本学习-8(sed命令学习)
- 【shell脚本】常用工具的学习grep、sed、awk、cut
- Shell脚本学习之sed详解
- shell脚本学习笔记 (sed的高级用法----模式空间和保持空间)
- shell脚本学习-6(sed命令学习)
- shell脚本学习-5(sed命令学习)
- Shell脚本学习之sed详解
- Shell脚本学习之sed详解
- shell脚本学习笔记之sed命令用法
- shell脚本学习-7(sed命令学习)
- Shell脚本学习笔记(七)--sed和awk
- shell脚本学习:正则表达式之sed,tr篇
- 学习笔记之Shell脚本学习指南 & sed与awk & 正则表达式
- shell脚本学习sed
- shell脚本学习笔记 (流编辑器sed)
- Shell脚本学习之sed详解
- shell脚本学习笔记(一)—— shell基础及sed
- Shell脚本学习之sed详解