1).删除文件每行的第一个字符(文件名为password.txt)
[root@localhost mxp]# sed -r 's/^(.)(.)/\2/' password.txt
2).删除文件每行的第二个字符
[root@localhost mxp]# sed -r 's/^(.)(.)(.)/\1\3/' password.txt
3)删除文件每行的最后一个字符
[root@localhost mxp]# sed -r 's/[[:alpha:]]+$//g' password.txt
4)删除文件每行的倒数第二个字符
[root@localhost mxp]# sed -r 's/(.)(.)(.)$/\1\3/g' password.txt
5)删除文件每行的第二个单词
[root@localhost mxp]# sed -r 's/([^:]+:)([^:]+)(:.)/\1\3/g' password.txt
6)删除文件每行的倒数第二个单词
[root@localhost mxp]# sed -r 's@(.:[^[:alpha:]])([[:alpha:]]+)([^[:alpha:]].)@\1\3@g' password.txt
7)删除文件每行的最后一个单词
[root@localhost mxp]# sed -r 's/[[:alpha:]]+$//g' password.txt
8)交换每行的第一个字符和第二个字符
[root@localhost mxp]# sed -r 's@^(.)(.)(.)@\2\1\3@g' password.txt
9)交换每行的第一个字符和第二个单词
[root@localhost mxp]# sed -r 's/^(.)([^:]+:)([^:]+)(:.)/\3\2\1\4/' password.txt
10)交换每行的第一个单词和最后一个单词
[root@localhost mxp]# sed -r 's/([^:]+)(.[^[:alpha:]])(.)/\3\2\1/' password.txt
11)删除一个文件中所有的数字
[root@localhost mxp]# sed -r 's@[[:digit:]]@@g' password.txt
12)删除每行开头的所有空格
[root@localhost mxp]# sed -r 's/^[[:space:]]+//g' password.txt
13)把所有大写字母用括号()括起来
[root@localhost mxp]# sed -r 's/[[:upper:]]/(&)/g' password.txt
14)打印每行3次
[root@localhost mxp]# sed 'p;p;p' -n password.txt
15)只显示每行的第一个单词
[root@localhost mxp]# sed -r 's/([^:]+)./\1/' password.txt
16)打印每行的第一个单词和第三个单词
[root@localhost mxp]# sed -r 's/([^:]+)(:[[:alpha:]]+:)([^:]+)(:.)/\1\3/' password.txt
17)用命令获取格式为 mm/yy/dd 的日期格式,结合管道,将其换成 mm;yy;dd格式
[root@localhost mxp]# date +%m/%y/%d|sed 's@/@-@g'
18).[root@localhost mxp]# sed -n '$=' password.txt#获取总行数
19).[root@localhost ~]# echo 2456736669234523457389988|sed -r ':a;s/([0-9]+)([0-9]{3})/\1,\2/;t a'#从后面起每3位加一个逗号
20).[root@localhost mxp]# sed -i G pass.txt #每行后面追加一行空行
21).[root@localhost mxp]# sed '3~3{s/^/#/}' password.txt#注释3n(n为自然数)行
centos7.4获取ip地址
[root@apenglinux-001 ~]# ifconfig|sed -n '/ens33/{n;p}'|sed -r -e 's/^[^[:digit:]]+//' -e 's/[[:space:]]+.*$//'
[root@apenglinux-001 ~]# ifconfig|sed -r -n '/ens33/{n; s/^[^[:digit:]]+//;p}'|sed -r 's/[[:space:]]+.*//'
有这样的文本bb
找出职位是jingli的人
分析:首先模式空间为zs,保持空间为空;经x之后,保持空间为zs,模式空间为空;n后模式空间为jingli;
如果模式空间为jingli,则进行互换;此时模式空间为人物的姓名,并打印出来,这样就找出职位为jingli
的人物了。
注意:始终打印的是模式空间的内容。
如果不是jingli,就复制一份数据给保持空间;如果是jingli,就互换数据;
要求:职位和用户名互换位置。
打印所有用户名
转载于:https://blog.51cto.com/13480443/2065572