码迷,mamicode.com
首页 > 系统相关 > 详细

Shell正则表达式和文本处理工具(一)

时间:2017-03-22 20:20:55      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:文本处理   大写   div   phone   sha   模板   efault   address   ges   

作业一:整理正则表达式博客

 

1、什么是正则
正则就是用一些具有特殊含义的符号组合而成(称为正则表达式)来描述字符或者字符串的方法。
或者说:正则就是用来描述一类事物的规则。

通配符是由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]
注意的一点是:如果要匹配的字符就是-本身话,必须放到最后去[123123\-]

 

作业二: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@luchuangao etc]# egrep -v ‘^$|^#‘ /etc/ssh/sshd_config   
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

 

Shell正则表达式和文本处理工具(一)

标签:文本处理   大写   div   phone   sha   模板   efault   address   ges   

原文地址:http://www.cnblogs.com/luchuangao/p/6601691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!