第十二章 正则表达式
grep [-acinv] '搜索字符串' filename
grep -n ‘the’ regular_express.txt
grep -vn ‘the’ regular_express.txt
grep -in ‘the’ regular_express.txt
grep -n 't[ae]st' regular_express.txt
grep -n 'oo' regular_express.txt
grep -n '[^g]oo' regular_express.txt
grep -n '[^a-z]oo' regular_express.txt
grep -n '[0-9]' regular_express.txt
grep -n '^[a-zA-Z]' regular_express.txt
grep -n '/.$' regular_express.txt
grep -n '^$' regular_express.txt
grep -v '^$' /etc/syslog.conf | grep -v '^$'
grep -n 'g..d' regular_express.txt
grep -n 'ooo*' regular_express.txt
grep -n 'goo*g' regular_express.txt
grep -n 'g*g' regular_express.txt
grep -n 'g.*g' regular_express.txt
grep -n '[0-9][0-9]*' regular_express.txt
grep -n 'o/{2/}' regular_express.txt
grep -n 'go/{2,5/}g' regular_express.txt
grep -n 'go/{2,/}g' regular_express.txt
egrep支持扩展正则表达式
+ ? | ()
. 任意一个字符
* 重复字符
{} 限定连续重复字符范围
[] 字符集合的重复特殊字符
格式化显示:printf
sed 工具 整行比较
sed [-nefr]
nl /etc/passwd | sed '2, 5d'
nl /etc/passwd | sed '2a drink tea'
nl /etc/passwd | sed '2a Drink tea or ...... > drink beer ?'
nl /etc/passwd | sed '2,5c No 2-5 number'
nl /etc/passwd | sed -n '5,7p'
ifconfig eth0 | grep 'inet' | sed 's/^.*addr://g' | sed
's/Bcast.*$//g'
cat /etc/man.config | grep 'MAN' | sed 's/#.*//g' | >
sed '/^$/d'
awk 用于一行中分成数个“字段”来处理
awk 条件1{动作1} 条件2{动作2} ... filename
last | awk '{print $1 "/t" $3}'
last | awk '{print $1 "/t lines: " NR "/t columes: " NF}'
cat /etc/passwd | awk 'BEGIN {FS=":"} $3 < 10 {print $1 "/t" $3}'
diff 文件比较
cmp 比较二进制
patch
习题:
1.要查找某个文件,其中含有boot字符串,这个文件在/etc下,如何找出
这个文件
答:ls /etc | grep 'boot'
2.在/etc下,只要含有XYZ3个字符的任何一个字符的那一行,就显示出来,
怎样进行
答:ls /etc [XYZ]
3.怎样找出在/etc下文件内容含有(*)的文件名
答:ls /etc | grep '/*'
grep [-acinv] '搜索字符串' filename
grep -n ‘the’ regular_express.txt
grep -vn ‘the’ regular_express.txt
grep -in ‘the’ regular_express.txt
grep -n 't[ae]st' regular_express.txt
grep -n 'oo' regular_express.txt
grep -n '[^g]oo' regular_express.txt
grep -n '[^a-z]oo' regular_express.txt
grep -n '[0-9]' regular_express.txt
grep -n '^[a-zA-Z]' regular_express.txt
grep -n '/.$' regular_express.txt
grep -n '^$' regular_express.txt
grep -v '^$' /etc/syslog.conf | grep -v '^$'
grep -n 'g..d' regular_express.txt
grep -n 'ooo*' regular_express.txt
grep -n 'goo*g' regular_express.txt
grep -n 'g*g' regular_express.txt
grep -n 'g.*g' regular_express.txt
grep -n '[0-9][0-9]*' regular_express.txt
grep -n 'o/{2/}' regular_express.txt
grep -n 'go/{2,5/}g' regular_express.txt
grep -n 'go/{2,/}g' regular_express.txt
egrep支持扩展正则表达式
+ ? | ()
. 任意一个字符
* 重复字符
{} 限定连续重复字符范围
[] 字符集合的重复特殊字符
格式化显示:printf
sed 工具 整行比较
sed [-nefr]
nl /etc/passwd | sed '2, 5d'
nl /etc/passwd | sed '2a drink tea'
nl /etc/passwd | sed '2a Drink tea or ...... > drink beer ?'
nl /etc/passwd | sed '2,5c No 2-5 number'
nl /etc/passwd | sed -n '5,7p'
ifconfig eth0 | grep 'inet' | sed 's/^.*addr://g' | sed
's/Bcast.*$//g'
cat /etc/man.config | grep 'MAN' | sed 's/#.*//g' | >
sed '/^$/d'
awk 用于一行中分成数个“字段”来处理
awk 条件1{动作1} 条件2{动作2} ... filename
last | awk '{print $1 "/t" $3}'
last | awk '{print $1 "/t lines: " NR "/t columes: " NF}'
cat /etc/passwd | awk 'BEGIN {FS=":"} $3 < 10 {print $1 "/t" $3}'
diff 文件比较
cmp 比较二进制
patch
习题:
1.要查找某个文件,其中含有boot字符串,这个文件在/etc下,如何找出
这个文件
答:ls /etc | grep 'boot'
2.在/etc下,只要含有XYZ3个字符的任何一个字符的那一行,就显示出来,
怎样进行
答:ls /etc [XYZ]
3.怎样找出在/etc下文件内容含有(*)的文件名
答:ls /etc | grep '/*'