grep
或者egrep -g
egreo
或者grep -e
sed:stream editor,流编辑工具程序。
awk:linux上是gawk,格式化文本工具程序。
grep:global search regular expression and print out the line
文本搜索工具,根据用户指定的搜索条件,对目标文本逐行扫描,打印出匹配的所有行。
搜索条件:就是用正则表达式来表示。
语法:
grep [options] pattern [file...]
grep [options][-e pattern | -f file] [file...]
最基本的例子:查找"uuid",在/etc/fstab
# grep "uuid" /etc/fstab uuid=3d3b316a-529e-484a-9895-e785fdde5365 /boot xfs defaults 0 0
搜索时,搜索条件的字母是区分大小写的,让它不区分大小写的选项:-i
# grep "uuid" /etc/fstab # echo $? 1 # grep -i "uuid" /etc/fstab uuid=3d3b316a-529e-484a-9895-e785fdde5365 /boot xfs defaults 0 0
不让它显示匹配到的一整行,只显示匹配但的文本内容本身:-o
# grep -o "uuid" /etc/fstab uuid
让它显示没有匹配到的行:-v
# grep -v "uuid" /etc/fstab /dev/mapper/centos-root / xfs defaults 0 0
不显示匹配到的内容,只想知道是否匹配的结果:-q
# grep -q "uuid" /etc/fstab # echo $? 0 # grep -q "uuida" /etc/fstab # echo $? 1
使用扩展正则表达式:-e
显示匹配到的行的行号:-n
# grep -n "uuid" /etc/fstab 10:uuid=3d3b316a-529e-484a-9895-e785fdde5365 /boot xfs defaults 0 0
显示匹配到行的后面几行:-a #
。#是数字
# grep -na1 gentoo /etc/passwd 49:gentoo:x:1004:1004::/tmp/gentoo:/bin/bash 50-fedora:x:1005:1005::/tmp/fedora:/bin/bash
显示匹配到行的前面几行:-b #
。#是数字
# grep -nb2 gentoo /etc/passwd 47-za2:x:1002:1003::/home/za2:/bin/bash 48-mysql:x:1003:979::/home/mysql:/sbin/nologin 49:gentoo:x:1004:1004::/tmp/gentoo:/bin/bas
显示匹配到行的前面几行和后面几行:-c #
。#是数字
# grep -nc1 gentoo /etc/passwd 48-mysql:x:1003:979::/home/mysql:/sbin/nologin 49:gentoo:x:1004:1004::/tmp/gentoo:/bin/bash 50-fedora:x:1005:1005::/tmp/fedora:/bin/bash
字符匹配
.:匹配任意单个字符
# grep -n "f..ora" /etc/passwd 50:fedora:x:1005:1005::/tmp/fedora:/bin/bash # grep "f.ora" /etc/passwd #
[]:匹配指定范围内的任意单个字符,中间不用逗号分隔
[^]:匹配指定范围外的任意单个字符
[:digit:],[:lower:],[:upper:],[:alpha:],[:alnum:],[:punct:],[:space:]
例子:匹配r和t之间,是2个字母的行。
# grep "r[[:alpha:]][[:alpha:]]t" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
匹配次数:默认是贪婪模式,匹配到后,还会一直继续匹配下去,直到匹配不到了才停。
下面的例子匹配"x*y",【xxxxy】里有很多x,贪婪模式就把所有x都匹配了,而不是匹配的【xy】。
【*】:匹配其前面的字符任意次。0次也包括。
注意下面方括号里的是被匹配到的。
# cat t1 abxy aby xxxxy yab asdf # grep "x*y" t1 ab[xy] ab[y] [xxxxy] [y]ab
匹配r本身和r之后面的所有字符。
# grep "r.*" /etc/passwd
【?】:匹配其前面的字符0次或者1次。
【\+】:匹配其前面的字符1次或者多次。
【\{m\}】:匹配其前面的字符m次。
【\{m,n\}】:匹配其前面的字符至少m次,至多n次。
【\{m,\}】:匹配其前面的字符至少m次.
【\{0,n\}】:匹配其前面的字符至多n次。
注意下面方括号里的是被匹配到的。
# cat t1 abxy aby xxxxy yab asdf # grep "x\?y" t1 ab[xy] ab[y] xxx[xy] [y]ab # grep "x\+y" t1 ab[xy] [xxxxy] # grep "x\{1\}y" t1 ab[xy] xxx[xy] # grep "x\{2\}y" t1 xx[xxy] # grep "x\{2,3\}y" t1 x[xxxy] # grep "x\{1,2\}y" t1 ab[xy] xx[xxy] # grep "x\{1,\}y" t1 ab[xy] [xxxxy] [root@localhost tmp]# grep "x\{,2\}y" t1 ab[xy] ab[y] xx[xxy] [y]ab
位置锚定
# grep root /etc/passwd [root]:x:0:0:[root]:/[root]:/bin/bash operator:x:11:0:operator:/[root]:/sbin/nologin [root]kit:x:1006:1006::/home/[root]kit:/bin/bash user4:x:1007:1007::/home/user4:/bin/ch[root] ch[root]er:x:1008:1008::/home/ch[root]er:/bin/bash # grep "^root" /etc/passwd [root]:x:0:0:root:/root:/bin/bash [root]kit:x:1006:1006::/home/rootkit:/bin/bash # grep "root$" /etc/passwd user4:x:1007:1007::/home/user4:/bin/ch[root] # grep "^root$" /etc/passwd # echo $? 1 # cat t1 abxy aby xxxxy yab asdf a # grep -n "^$" t1 3: # grep -n "^[[:space:]]*$" t1 3: 5: # grep -n "^[[:space:]]\+$" t1 5: # grep "\<root" /etc/passwd [root]:x:0:0:[root]:/[root]:/bin/bash operator:x:11:0:operator:/[root]:/sbin/nologin [root]kit:x:1006:1006::/home/[root]kit:/bin/bash # grep "root\>" /etc/passwd [root]:x:0:0:[root]:/[root]:/bin/bash operator:x:11:0:operator:/[root]:/sbin/nologin user4:x:1007:1007::/home/user4:/bin/ch[root] # grep "\<root\>" /etc/passwd [root]:x:0:0:[root]:/[root]:/bin/bash operator:x:11:0:operator:/[root]:/sbin/nologin
练习1:显示/etc/passwd文件中不以/bin/bash结尾的行
# grep -nv "/bin/bash$" /etc/passwd
练习2:找出/etc/passwd文件中2位数或3位数的单词。
# grep -n "\<[[:digit:]]\{2,3\}\>" /etc/passwd
练习3:找出/etc/grub2.cfg文件中,以至少一个空白字符开头,且后面非空白字符的行。
# grep -n "^[[:space:]]\{1,\}[^[:space:]]" /etc/grub2.cfg
练习4:找出"netstat -tan"命令结果中以"listen"后跟0个,1个或多个空白字符结尾的行
# netstat -tan | grep -n "listen[[:space:]]*"
分组及引用
分组【\(\)】:将一个或多个字符用括号捆绑在一起,当作一个整体去匹配。
引用:被匹配到的分组,会保存在特殊的变量里,在后面可以引用它们。
练习:匹配一个分组,且后面有一个同样的串。
# cat t2 he likes his lover. he loves his lover. she likes her liker. she loves her liker. # grep "l..e.*l..e" t2 he [likes his love]r. he [loves his love]r. she [likes her like]r. she [loves her like]r. # grep "\(l..e\).*\1" t2 he [loves his love]r. she [likes her like]r.
grep里的选项的用法在egrep里也适用。
字符匹配:和grep相同
次数匹配
位置锚定:和grep相同
分组及引用
或
练习1:找出/proc/meminfo文件中,所有在大写或小写s开头的行。用3种方法实现。
# egrep "^(s|s)" /proc/meminfo
# grep -ni "^s" /proc/meminfo
# grep "^[ss]" /proc/meminfo
练习2:找出/etc/passwd文件中2位数或3位数的单词。
# egrep -n "\<[[:digit:]]{2,3}\>" /etc/passwd
练习3:找出/etc/grub2.cfg文件中,以至少一个空白字符开头,且后面非空白字符的行。
# egrep -n "^[[:space:]]{1,}[^[:space:]]" /etc/grub2.cfg
练习4:找出/etc/rc.d/init.d/functions文件中某单词后面跟一个小括号的行。
# grep "\<.*\>[[:space:]]*()" /etc/rc.d/init.d/functions
练习5:使用echo命令输出一个绝对路径,使用egrep取出基名。
# echo /etc/rc.d/init.d/functions | grep -o "^/.*/" /etc/rc.d/init.d/ # echo /etc/rc.d/init.d/functions | egrep -o "[^/]+$" functions
练习6:找出ifconfig命令结果中1-255之间的数值。
# ifconfig | grep -e "\<[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]\>"
练习7:找出ifconfig命令结果中ip地址。
# ifconfig | egrep -n "\<[0-9]+\>.\<[0-9]+\>.\<[0-9]+\>.\<[0-9]+\>"
练习8:找出用户名和shell名相同的用户。
# egrep "^([^:]+\>).*\1$" /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt
当无需使用到正则表达式时,使用fgrep性能更好。
1,wc:统计行数,单词数,字节数,字符数
# wc /etc/fstab 12 60 541 /etc/fstab # wc -l /etc/fstab 12 /etc/fstab # wc -w /etc/fstab 60 /etc/fstab # wc -c /etc/fstab 541 /etc/fstab # wc -m /etc/fstab 541 /etc/fstab
2,remove sections(列) from each line of files
linux下的文本,也是有格式的,所谓的格式,就是有可识别的分隔标识,用分隔标识,就可以把文本内容,切分成列。
比如,/etc/passwdwen文件里的内容就是用冒号分隔的。
语法:cut option... [file]...
指定冒号为分隔符:-d:
只能指定单一分隔符。
留下哪些列:-f1-3,5,7
# cut -d: -f1-3,5,7 /etc/passwd rootkit:x:1006::/bin/bash user4:x:1007::/bin/chroot # wc -l /etc/rc.d/init.d/functions 712 /etc/rc.d/init.d/functions # wc -l /etc/rc.d/init.d/functions | cut -d' ' -f1 712
3,按文本的某一列排序:sort。
把文本用指定的分隔符切分成列,然后用特定的列排序行。类似微软的excel的按列排序功能。
sort [option]... [file]...
用:分隔,按第3列的数字大小比较,降序排序。
# sort -t: -k3 -nr /etc/passwd
用:分隔,用第7列基于字母比较,升序排序,并去掉重复的行。
# sort -t: -k7 -u /etc/passwd
4,删除重复的行:uniq
使用的前提:必须先sort
uniq [option]... [input [output]]
检查shell的使用情况。
# cut -d: -f7 /etc/passwd | sort |uniq -c 7 /bin/bash 1 /bin/chroot 1 /bin/csh 1 /bin/false 1 /bin/sync 1 /sbin/halt 40 /sbin/nologin 1 /sbin/shutdown # cut -d: -f7 /etc/passwd | sort |uniq -u /bin/chroot /bin/csh /bin/false /bin/sync /sbin/halt /sbin/shutdown # cut -d: -f7 /etc/passwd | sort |uniq -d /bin/bash /sbin/nologin
5,逐行比较文件,可以比较多个文件,可以按目录比较
diff [option]... files
# diff t1 t2 # diff t1 t2 > patch1
6,根据diff产生的差分文件,给源文件打补丁:patch
# patch -i patch1 t1
-r
# patch -r -i patch1 t1
练习:取出某个网卡的ip地址。
# ifconfig enp0s3 enp0s3: flags=4163<up,broadcast,running,multicast> mtu 1500 inet 10.247.236.19 netmask 255.255.254.0 broadcast 10.247.237.255 inet6 fe80::b497:5ec:1efb:72b5 prefixlen 64 scopeid 0x20<link> ether 08:00:27:10:c2:53 txqueuelen 1000 (ethernet) rx packets 32057 bytes 5882570 (5.6 mib) rx errors 0 dropped 0 overruns 0 frame 0 tx packets 5324 bytes 1032770 (1008.5 kib) tx errors 0 dropped 0 overruns 0 carrier 0 collisions 0 # ifconfig enp0s3 | grep "\<inet\>" | cut -d' ' -f10 10.247.236.19
c/c++ 学习互助qq群:877684253
本人微信:xiaoshitou5854
如对本文有疑问, 点击进行留言回复!!
linux下文本编辑器vim的使用方法(复制、粘贴、替换、行号、撤销、多文件操作)
网友评论