REGULAR EXPRESSION
1. 正则表达式和通配符是不同的两个概念
通配符的反向选择为!,而RE中为^
通配符中?代表单个字符,而RE中.代表单个字符
2. grep是最常用的利用RE的工具
grep -option 'targetString' filename
-c: 计算找到的次数
-i: 忽略大小写
-n:输出行号
-v: 反向选择,即显示不符合搜索条件的行
fredchen@fredchen:~$ grep -n 't[ae]st' regular_express
8:I can't finish the test.
9:Oh! The soup taste good.
1) [ae]: a或e
2) '[^g]oo' : 00前面不是g
fredchen@fredchen:~$ grep -n '[^g]oo' regular_express
2:apple is my favorite food.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!
注意最后两行,虽然存在oo前面含有g的字符串但还是被筛选,因为有满足条件的字符串存在!
3)行首与行尾^, $
fredchen@fredchen:~$ grep -n '^the' regular_express
12:the symbol '^' is represented as start.
^在[]内外的含义完全不同,要注意
行首不是字母的行
fredchen@fredchen:~$ grep -n '^[^a-zA-Z]' regular_express
1:"Open source" is a good mechanism to develop programs.
21:# I am VBird
以.结尾的
fredchen@fredchen:~$ grep -n '/.$' regular_express
1:"Open source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
5:However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
找出空白行:相当于只有行尾和行首
fredchen@fredchen:~$ grep -n '^$' regular_express
22:
去掉代码的空行和注释行:
fredchen@fredchen:~$ grep -v '^$' /etc/syslog.conf | grep -v '^#'
auth,authpriv.* /var/log/auth.log
*.*;auth,authpriv.none -/var/log/syslog
daemon.* -/var/log/daemon.log
kern.* -/var/log/kern.log
lpr.* -/var/log/lpr.log
mail.* -/var/log/mail.log
user.* -/var/log/user.log
mail.info -/var/log/mail.info
mail.warning -/var/log/mail.warn
mail.err /var/log/mail.err
news.crit /var/log/news/news.crit
news.err /var/log/news/news.err
news.notice -/var/log/news/news.notice
*.=debug;/
auth,authpriv.none;/
news.none;mail.none -/var/log/debug
*.=info;*.=notice;*.=warning;/
auth,authpriv.none;/
cron,daemon.none;/
mail,news.none -/var/log/messages
*.emerg *
daemon.*;mail.*;/
news.err;/
*.=debug;*.=info;/
*.=notice;*.=warning |/dev/xconsole
4)任意字符. 重复字符*
注:*可以表示空字符 ,如x*则表示0个或多个x。而xx*才表示一个或多个
fredchen@fredchen:~$ grep -n 'g..d' regular_express
1:"Open source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".
含有两个或多个o的行
fredchen@fredchen:~$ grep -n 'ooo*' regular_express
1:"Open source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!
含g...g型的字符串的行
fredchen@fredchen:~$ grep -n 'g.*g' regular_express
1:"Open source" is a good mechanism to develop programs.
14:The gd software is a library for drafting programs.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
任意数字'[0-9][0-9]*'
5) 限定连续重复字符范围
2个到5个o的goo..g型字符串
fredchen@fredchen:~$ grep -n 'go/{2,5/}g' regular_express
18:google is the best tools for search keyword.
如果确定重复数,则显然可以用*更方便
3. 文件比较diff
-i:忽略大小写
-b:忽略一行中多个空白的差异
-B:忽略空白行的差异
fredchen@fredchen:~$ diff newfile new2
3d2
< zyx
fredchen@fredchen:~$ cat newfile
I'm fredchen
This is the last line
zyx
ujl
rejrl
fredchen@fredchen:~$ cat new2
I'm fredchen
This is the last line
ujl
rejrl
4. cmp 也可用于文件比较,不过用得不如diff广泛
diff一行为单位比较,cmp按位比较,可用于比较二进制文件
5. 打印机打印: pr
pr /etc/man.config