Shell正则表达式和文本处理工具
作业一:整理正则表达式博客
一、什么是正则
正则就是用一些具有特殊含义的符号组合而成(称为正则表达式)来描述字符或者字符串的方法。
或者说:正则就是用来描述一类事物的规则。
通配符是由shell解释得。如果shell是爷爷,通配符就是爹。
正则表达式则是由命令解释得,命令又被shell解释。如果shell是爷爷,正则表达式就是孙子。
通配符和命令是一个级别的。
通配符是为了方便命令操作,正则表达式是为了操作文本内容、字符或字符串。
正则介绍:
^行首
$行尾
.除了换行符以外的任意单个字符
.*所有字符
*:左边的那一个字符有0个到无穷个
+:左边的那一个字符有1个到无穷个
?:左边的那一个字符有0个到1个
{n}:左边的那一个字符有n个
{n,m}:左边的那一个字符有n个到m个
{n,}:左边的那一个字符有n个到无穷个
[]字符组内的任一字符
[^]对字符组内的每个字符取反(不匹配字符组内的每个字符)
^[^]非字符组内的字符开头的行
[a-z]:所有的小写字母
[A-Z]:所有的大写字母
[a-zA-Z]:所有的大小写字母,等于[a-Z]
[0-9]:数字
\<单词头 单词一般以空格或特殊字符做分隔,连续的字符串被当做单词
\>单词尾
注意的一点是:如果要匹配的字符就是-本身话,必须放到最后去[123123\-]
扩展正则sed 加 -r参数或转义
grep 加-E或egrep或转义
awk直接支持,但不包含{n,m}
可以使用--posix支持
awk '/ro{1,3}/{print}' /etc/passwd
awk --posix '/ro{1,3}/{print}' /etc/passwd
sed -n '/roo\?/p' /etc/passwd
sed -rn '/roo?/p' /etc/passwd
?前导字符零个或一个
+前导字符一个或多个
abc|def abc或def
a(bc|de)f abcf或adef
x\{m\}x出现m次
x\{m,\}x出现m次至多次(至少m次)
x\{m,n\}x出现m次至n次
posix定义的字符分类:
[:alnum:]Alphanumeric characters
匹配范围为[a-zA-Z0-9]
[:alpha:]Alphabic characters
匹配范围为[a-zA-Z]
[:blank:]Space or tab characters
匹配范围为 空格和TAB键
[:cntrl:]Control characters
匹配控制键 例如^M要按ctrl+v再按回车 才能输出
[:digit:]Numeric characters
匹配所有数字[0-9]
[:graph:]Characters that are both printable and visible(A space is print-able,but not visible,while an a is both)
匹配所有可见字符 但不包含空格和TAB 就是你在文本文档中按键盘上能用眼睛观察到的所有符号
[:lower:]Lower-case alphabetic characters
匹配所有可见字符,包括空格和TAB
能打印到纸上的所有符号
[:punct:]Punctuation characters(characters that are not letter,digits,control characters,or space characters)
特殊输入符号 +-=)(*&^%$#@!~`|\"'{}[]:;?/>.<,
注意它不包含空格和TAB
这个集合不等于^[a-zA-Z0-9]
[:space:] Space characters (such as space, tab, and formfeed, to name a few).
[:upper:] Upper-case alphabetic characters.
大写 [A-Z]
[:xdigit:] Characters that are hexadecimal digits.
16进制数 [0-f]
使用方法:
[root@seker ~]# grep --color '[[:alnum:]]' /etc/passwd
二、grep
参数
-n:显示行号 -o:只显示匹配的内容 -q:静默模式,没有任何输出,得用$?来判断执行成功没有,即有没有过滤到想要的内容 -l:如果匹配成功,则只将文件名打印出来,失败则不打印,通常-rl一起用,grep -rl 'root' /etc -A:如果匹配成功,则将匹配行及其后n行一起打印出来 -B:如果匹配成功,则将匹配行及其前n行一起打印出来 -C:如果匹配成功,则将匹配行及其前后n行一起打印出来 --color -c:如果匹配成功,则将匹配到的行数打印出来 -E:等于egrep,扩展 -i:忽略大小写 -v:取反,不匹配 -w:匹配单词
grep种类
grep fgrep pgrep egrep
三、sed
sed流编辑器 stream editer,是以行为单位的处理程序
语法
sed [options] 'command' in_file[s] options 部分 -n -e -i -f command 部分 '[地址1,地址2][函数][参数(标记)]'
定址的方法 1.数字 2.正则
数字 十进制数 1 单行 1,3范围 从第一行到第三行 2,+4匹配行后若干行 4,~3从第四行到下一个3的倍数行 $ 尾行 1!除了第一行以外的行 正则 正则必须用//包裹起来 扩展正则需要用-r参数或转义 数字定址:sed -n '1p' /etc/passwd 正则定址:sed -n '/^root/p' /etc/password
函数 增删改 a 后插 c 替换 i 前插 d 删除 输入输出 p 打印匹配的行一般和-n参数连用,以屏蔽默认输出 r 从文件中读入 w 写入到文件中 控制流 !命令取反 例:1!d删除第一行以外的行 {}命令组合 命令用分号分隔{1h;G}可以理解为-e参数的另一种写法 =打印行号(输入行的号码,而非处理的次数行号)例如:sed -n '2{=;p}' infile n读入下一行到模式空间 例:'4{n;d}' 删除第5行 N而是追加下一行到模式空间,再把当前行和下一行同时应用后面的命令
替换
s 字符串替换 s/old/new/ sed -n 's/root/ABCDEF/p' /etc/passwd sed -n 's/root/ABCDEF/gp' /etc/passwd sed -n 's/root/ABCDEF/2p' /etc/passwd sed -n 's/root/ABCDEF/3p' /etc/passwd sed -n 's/root/ABCDEF/gp' /etc/passwd
$
\(\)保存被匹配的字符 以备反向引用\N时使用 最多9个标签 标签顺序从左到右
& 替换时使用,在不定义标签时使用(反向引用)
试做:
删除第一个单词
删除最后一个单词
将第一个单词和最后一个单词兑换位置
$ sed '1{p;p}' a.txt 11111111 11111111 11111111 22222222 33333333 44444444 55555555 66666666
作业二:grep作业(正则表达式及字符处理)
目标文件/etc/passwd,使用grep命令或egrep
1.显示出所有含有root的行:
[root@luchuangao ~]# egrep 'root' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
2.输出任何包含bash的所有行,还要输出紧接着这行的上下各两行的内容:
[root@luchuangao ~]# egrep -A 2 'bash' /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin -- luchuan:x:1001:1001::/home/luchuan:/bin/bash egon:x:1002:1002::/home/egon:/bin/bash tom:x:1003:1003::/home/tom:/bin/bash aa:x:1004:1004::/home/aa:/bin/bash luchuangao:x:1005:1005::/home/luchuangao:/bin/bash alex:x:1006:1006::/home/alex:/bin/bash jack:x:1007:1008::/home/jack:/bin/bash rose:x:1008:1009::/home/rose:/bin/bash apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin dhcpd:x:177:177:DHCP server:/:/sbin/nologin
3. 显示出有多少行含有nologin。
[root@luchuangao ~]# egrep -c 'root' /etc/passwd 2
4.显示出那些行含有root,并将行号一块输出。
[root@luchuangao ~]# egrep -n 'root' /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin
5.显示出文件名
[root@luchuangao ~]# egrep -l 'root' /etc/passwd /etc/passwd
6.新建用户
abominable
abominate
anomie
atomize
编写正则表达式,将他们匹配出来
[root@luchuangao ~]# egrep '^a[a-z]o[a-z]+e' /etc/passwd abominable:x:1009:1010::/home/abominable:/bin/bash abominate:x:1010:1011::/home/abominate:/bin/bash anomie:x:1011:1012::/home/anomie:/bin/bash atomize:x:1012:1013::/home/atomize:/bin/bash
7.建四个用户
Alex213sb
Wpq2222b
yH438PIG
egon666
egon
过滤出用户名组成是字母+数字+字母的行
[root@luchuangao ~]# egrep '[a-Z]+[0-9]+[a-Z]+' /etc/passwd Alex213sb:x:1013:1014::/home/Alex213sb:/bin/bash Wpq2222b:x:1014:1015::/home/Wpq2222b:/bin/bash yH438PIG:x:1015:1016::/home/yH438PIG:/bin/bash
8.显示出/etc目录下所有包含root的文件名
[root@nfs-server ~]# grep -rl 'root' /etc/* | grep 'root' /etc/security/chroot.conf
9. 过滤掉/etc/ssh/sshd_config内所有注释和所有空行
[root@nfs-server ~]# egrep -v "^#" /etc/ssh/sshd_config | egrep -v "^ *$" # 也可以使用"^#|^$",但是不过滤空格 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication yes ChallengeResponseAuthentication no GSSAPIAuthentication yes GSSAPICleanupCredentials no UsePAM yes X11Forwarding yes UsePrivilegeSeparation sandbox # Default for new installations. AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS Subsystem sftp /usr/libexec/openssh/sftp-server
作业三:Sed作业:以/etc/passwd文件为模板
1,删除文件每行的第一个字符。
[root@luchuangao ~]# sed -r 's/^(.)(.*)$/\2/' test oot:x:0:0:root:/root:/bin/bash in:x:1:1:bin:/bin:/sbin/nologin ginx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologin bominable:x:1009:1010::/home/abominable:/bin/bash bominate:x:1010:1011::/home/abominate:/bin/bash nomie:x:1011:1012::/home/anomie:/bin/bash tomize:x:1012:1013::/home/atomize:/bin/bash lex213sb:x:1013:1014::/home/Alex213sb:/bin/bash pq2222b:x:1014:1015::/home/Wpq2222b:/bin/bash H438PIG:x:1015:1016::/home/yH438PIG:/bin/bash gon666:x:1016:1017::/home/egon666:/bin/bash gon:x:1017:1002::/home/egon:/bin/bash
2,删除文件每行的第二个字符。
[root@luchuangao ~]# sed -r 's/^(.)(.)(.*)$/\1\3/' test rot:x:0:0:root:/root:/bin/bash bn:x:1:1:bin:/bin:/sbin/nologin ninx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologin aominable:x:1009:1010::/home/abominable:/bin/bash aominate:x:1010:1011::/home/abominate:/bin/bash aomie:x:1011:1012::/home/anomie:/bin/bash aomize:x:1012:1013::/home/atomize:/bin/bash Aex213sb:x:1013:1014::/home/Alex213sb:/bin/bash Wq2222b:x:1014:1015::/home/Wpq2222b:/bin/bash y438PIG:x:1015:1016::/home/yH438PIG:/bin/bash eon666:x:1016:1017::/home/egon666:/bin/bash eon:x:1017:1002::/home/egon:/bin/bash
3,删除文件每行的最后一个字符。
[root@luchuangao ~]# sed -r 's/([^a-Z])(.*)(.)$/\1\2/' test root:x:0:0:root:/root:/bin/bas bin:x:1:1:bin:/bin:/sbin/nologi nginx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologi abominable:x:1009:1010::/home/abominable:/bin/bas abominate:x:1010:1011::/home/abominate:/bin/bas anomie:x:1011:1012::/home/anomie:/bin/bas atomize:x:1012:1013::/home/atomize:/bin/bas Alex213sb:x:1013:1014::/home/Alex213sb:/bin/bas Wpq2222b:x:1014:1015::/home/Wpq2222b:/bin/bas yH438PIG:x:1015:1016::/home/yH438PIG:/bin/bas egon666:x:1016:1017::/home/egon666:/bin/bas egon:x:1017:1002::/home/egon:/bin/bas
4,删除文件每行的倒数第二个字符。
[root@luchuangao ~]# sed -r 's/([^a-Z])(.*)(.)(.)$/\1\2\4/' test root:x:0:0:root:/root:/bin/bah bin:x:1:1:bin:/bin:/sbin/nologn nginx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologn abominable:x:1009:1010::/home/abominable:/bin/bah abominate:x:1010:1011::/home/abominate:/bin/bah anomie:x:1011:1012::/home/anomie:/bin/bah atomize:x:1012:1013::/home/atomize:/bin/bah Alex213sb:x:1013:1014::/home/Alex213sb:/bin/bah Wpq2222b:x:1014:1015::/home/Wpq2222b:/bin/bah yH438PIG:x:1015:1016::/home/yH438PIG:/bin/bah egon666:x:1016:1017::/home/egon666:/bin/bah egon:x:1017:1002::/home/egon:/bin/bah [root@luchuangao ~]#
5,删除文件每行的第二个单词。
[root@luchuangao ~]# sed -r 's/(^[a-Z0-9]+)([^a-Z0-9])([a-Z0-9]+)([^a-Z0-9])/\1\2\4/g' test root::0:0:root:/root:/bin/bash bin::1:1:bin:/bin:/sbin/nologin nginx::991:547:Nginx web server:/var/lib/nginx:/sbin/nologin abominable::1009:1010::/home/abominable:/bin/bash abominate::1010:1011::/home/abominate:/bin/bash anomie::1011:1012::/home/anomie:/bin/bash atomize::1012:1013::/home/atomize:/bin/bash Alex213sb::1013:1014::/home/Alex213sb:/bin/bash Wpq2222b::1014:1015::/home/Wpq2222b:/bin/bash yH438PIG::1015:1016::/home/yH438PIG:/bin/bash egon666::1016:1017::/home/egon666:/bin/bash egon::1017:1002::/home/egon:/bin/bash [root@luchuangao ~]#
6,删除文件每行的倒数第二个单词。
[root@luchuangao ~]# sed -r 's/([^a-Z0-9])([a-Z0-9]+)([^a-Z0-9])([a-Z0-9]+$)/\1\3\4/g' test root:x:0:0:root:/root://bash bin:x:1:1:bin:/bin://nologin nginx:x:991:547:Nginx web server:/var/lib/nginx://nologin abominable:x:1009:1010::/home/abominable://bash abominate:x:1010:1011::/home/abominate://bash anomie:x:1011:1012::/home/anomie://bash atomize:x:1012:1013::/home/atomize://bash Alex213sb:x:1013:1014::/home/Alex213sb://bash Wpq2222b:x:1014:1015::/home/Wpq2222b://bash yH438PIG:x:1015:1016::/home/yH438PIG://bash egon666:x:1016:1017::/home/egon666://bash egon:x:1017:1002::/home/egon://bash
7,删除文件每行的最后一个单词。
[root@luchuangao ~]# sed -r 's/([^a-Z0-9])([a-Z0-9]+$)/\1/g' test root:x:0:0:root:/root:/bin/ bin:x:1:1:bin:/bin:/sbin/ nginx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/ abominable:x:1009:1010::/home/abominable:/bin/ abominate:x:1010:1011::/home/abominate:/bin/ anomie:x:1011:1012::/home/anomie:/bin/ atomize:x:1012:1013::/home/atomize:/bin/ Alex213sb:x:1013:1014::/home/Alex213sb:/bin/ Wpq2222b:x:1014:1015::/home/Wpq2222b:/bin/ yH438PIG:x:1015:1016::/home/yH438PIG:/bin/ egon666:x:1016:1017::/home/egon666:/bin/ egon:x:1017:1002::/home/egon:/bin/
8,交换每行的第一个字符和第二个字符。
[root@luchuangao ~]# sed -r 's/^(.)(.)/\2\1/' test orot:x:0:0:root:/root:/bin/bash ibn:x:1:1:bin:/bin:/sbin/nologin gninx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologin baominable:x:1009:1010::/home/abominable:/bin/bash baominate:x:1010:1011::/home/abominate:/bin/bash naomie:x:1011:1012::/home/anomie:/bin/bash taomize:x:1012:1013::/home/atomize:/bin/bash lAex213sb:x:1013:1014::/home/Alex213sb:/bin/bash pWq2222b:x:1014:1015::/home/Wpq2222b:/bin/bash Hy438PIG:x:1015:1016::/home/yH438PIG:/bin/bash geon666:x:1016:1017::/home/egon666:/bin/bash geon:x:1017:1002::/home/egon:/bin/bash
9,交换每行的第一个字符和第二个单词。
[root@luchuangao ~]# sed -r 's/(^[a-Z])([a-Z0-9]+)([^a-Z0-9])([a-Z]+)/\4\2\3\1/' test xoot:r:0:0:root:/root:/bin/bash xin:b:1:1:bin:/bin:/sbin/nologin xginx:n:991:547:Nginx web server:/var/lib/nginx:/sbin/nologin xbominable:a:1009:1010::/home/abominable:/bin/bash xbominate:a:1010:1011::/home/abominate:/bin/bash xnomie:a:1011:1012::/home/anomie:/bin/bash xtomize:a:1012:1013::/home/atomize:/bin/bash xlex213sb:A:1013:1014::/home/Alex213sb:/bin/bash xpq2222b:W:1014:1015::/home/Wpq2222b:/bin/bash xH438PIG:y:1015:1016::/home/yH438PIG:/bin/bash xgon666:e:1016:1017::/home/egon666:/bin/bash xgon:e:1017:1002::/home/egon:/bin/bash
10,交换每行的第一个单词和最后一个单词。
[root@luchuangao ~]# sed -r 's/(^[a-Z0-9]+)(.*)([^a-Z0-9])([a-Z]+$)/\4\2\3\1/' test bash:x:0:0:root:/root:/bin/root nologin:x:1:1:bin:/bin:/sbin/bin nologin:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nginx bash:x:1009:1010::/home/abominable:/bin/abominable bash:x:1010:1011::/home/abominate:/bin/abominate bash:x:1011:1012::/home/anomie:/bin/anomie bash:x:1012:1013::/home/atomize:/bin/atomize bash:x:1013:1014::/home/Alex213sb:/bin/Alex213sb bash:x:1014:1015::/home/Wpq2222b:/bin/Wpq2222b bash:x:1015:1016::/home/yH438PIG:/bin/yH438PIG bash:x:1016:1017::/home/egon666:/bin/egon666 bash:x:1017:1002::/home/egon:/bin/egon
11,删除一个文件中所有的数字。
[root@luchuangao ~]# sed -r 's/([0-9]+)//g' test root:x:::root:/root:/bin/bash bin:x:::bin:/bin:/sbin/nologin nginx:x:::Nginx web server:/var/lib/nginx:/sbin/nologin abominable:x::::/home/abominable:/bin/bash abominate:x::::/home/abominate:/bin/bash anomie:x::::/home/anomie:/bin/bash atomize:x::::/home/atomize:/bin/bash Alexsb:x::::/home/Alexsb:/bin/bash Wpqb:x::::/home/Wpqb:/bin/bash yHPIG:x::::/home/yHPIG:/bin/bash egon:x::::/home/egon:/bin/bash egon:x::::/home/egon:/bin/bash
12,删除每行开头的所有空格。
[root@luchuangao ~]# cat test root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin nginx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologin abominable:x:1009:1010::/home/abominable:/bin/bash abominate:x:1010:1011::/home/abominate:/bin/bash anomie:x:1011:1012::/home/anomie:/bin/bash atomize:x:1012:1013::/home/atomize:/bin/bash Alex213sb:x:1013:1014::/home/Alex213sb:/bin/bash Wpq2222b:x:1014:1015::/home/Wpq2222b:/bin/bash yH438PIG:x:1015:1016::/home/yH438PIG:/bin/bash egon666:x:1016:1017::/home/egon666:/bin/bash egon:x:1017:1002::/home/egon:/bin/bash [root@luchuangao ~]# [root@luchuangao ~]# sed -r 's/(^[[:blank:]]+)//g' test root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin nginx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologin abominable:x:1009:1010::/home/abominable:/bin/bash abominate:x:1010:1011::/home/abominate:/bin/bash anomie:x:1011:1012::/home/anomie:/bin/bash atomize:x:1012:1013::/home/atomize:/bin/bash Alex213sb:x:1013:1014::/home/Alex213sb:/bin/bash Wpq2222b:x:1014:1015::/home/Wpq2222b:/bin/bash yH438PIG:x:1015:1016::/home/yH438PIG:/bin/bash egon666:x:1016:1017::/home/egon666:/bin/bash egon:x:1017:1002::/home/egon:/bin/bash
13,用制表符替换文件中出现的所有空格。
[root@luchuangao ~]# sed -r 's/ /\t/g' test root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin nginx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologin abominable:x:1009:1010::/home/abominable:/bin/bash abominate:x:1010:1011::/home/abominate:/bin/bash anomie:x:1011:1012::/home/anomie:/bin/bash atomize:x:1012:1013::/home/atomize:/bin/bash Alex213sb:x:1013:1014::/home/Alex213sb:/bin/bash Wpq2222b:x:1014:1015::/home/Wpq2222b:/bin/bash yH438PIG:x:1015:1016::/home/yH438PIG:/bin/bash egon666:x:1016:1017::/home/egon666:/bin/bash egon:x:1017:1002::/home/egon:/bin/bash
14,把所有大写字母用括号()括起来。
[root@luchuangao ~]# sed -r 's/[A-Z]/(&)/g' test root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin nginx:x:991:547:(N)ginx web server:/var/lib/nginx:/sbin/nologin abominable:x:1009:1010::/home/abominable:/bin/bash abominate:x:1010:1011::/home/abominate:/bin/bash anomie:x:1011:1012::/home/anomie:/bin/bash atomize:x:1012:1013::/home/atomize:/bin/bash (A)lex213sb:x:1013:1014::/home/(A)lex213sb:/bin/bash (W)pq2222b:x:1014:1015::/home/(W)pq2222b:/bin/bash y(H)438(P)(I)(G):x:1015:1016::/home/y(H)438(P)(I)(G):/bin/bash egon666:x:1016:1017::/home/egon666:/bin/bash egon:x:1017:1002::/home/egon:/bin/bash
15,打印每行3次。
[root@luchuangao ~]# sed -r 'p;p' test root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin nginx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologin nginx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologin nginx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologin abominable:x:1009:1010::/home/abominable:/bin/bash abominable:x:1009:1010::/home/abominable:/bin/bash abominable:x:1009:1010::/home/abominable:/bin/bash abominate:x:1010:1011::/home/abominate:/bin/bash abominate:x:1010:1011::/home/abominate:/bin/bash abominate:x:1010:1011::/home/abominate:/bin/bash anomie:x:1011:1012::/home/anomie:/bin/bash anomie:x:1011:1012::/home/anomie:/bin/bash anomie:x:1011:1012::/home/anomie:/bin/bash atomize:x:1012:1013::/home/atomize:/bin/bash atomize:x:1012:1013::/home/atomize:/bin/bash atomize:x:1012:1013::/home/atomize:/bin/bash Alex213sb:x:1013:1014::/home/Alex213sb:/bin/bash Alex213sb:x:1013:1014::/home/Alex213sb:/bin/bash Alex213sb:x:1013:1014::/home/Alex213sb:/bin/bash Wpq2222b:x:1014:1015::/home/Wpq2222b:/bin/bash Wpq2222b:x:1014:1015::/home/Wpq2222b:/bin/bash Wpq2222b:x:1014:1015::/home/Wpq2222b:/bin/bash yH438PIG:x:1015:1016::/home/yH438PIG:/bin/bash yH438PIG:x:1015:1016::/home/yH438PIG:/bin/bash yH438PIG:x:1015:1016::/home/yH438PIG:/bin/bash egon666:x:1016:1017::/home/egon666:/bin/bash egon666:x:1016:1017::/home/egon666:/bin/bash egon666:x:1016:1017::/home/egon666:/bin/bash egon:x:1017:1002::/home/egon:/bin/bash egon:x:1017:1002::/home/egon:/bin/bash egon:x:1017:1002::/home/egon:/bin/bash
16,只显示每行的第一个单词。
[root@luchuangao ~]# sed -r 's/(^[a-Z0-9]+)([^a-Z])(.*)/\1/' test root bin nginx abominable abominate anomie atomize Alex213sb Wpq2222b yH438PIG egon666 egon
17,打印每行的第一个单词和第三个单词。
[root@luchuangao ~]# sed -r 's/^([a-Z0-9]+)([^a-Z])([a-Z0-9]+)([^a-Z])([a-Z0-9]+)([^a-z])(.*)/\1\5/' test root0 bin1 nginx991 abominable1009 abominate1010 anomie1011 atomize1012 Alex213sb1013 Wpq2222b1014 yH438PIG1015 egon6661016 egon1017
18,用命令获取格式为 mm/yy/dd 的日期格式,结合管道,将其换成 mm;yy;dd格式
参考链接:http://www.cnblogs.com/linhaifeng/p/6596660.html