查找、过滤文件

一、搜索文件内容

一、grep 过滤文件内容

​ 作用:从文件中搜索出符合条件的内容

格式:
# grep [选项] "pattern"  文件名称

pattern:  条件 
	由普通字符、正则表达式组成的条件 
	
默认会显示符合条件的整行内容 

[root@localhost ~]# grep "root" /etc/passwd 

[root@localhost ~]# ifconfig ens33 | grep "netmask" 

二、正则表达式 Regex

​ 由一类特殊的字符【元字符】组成的表达式, 用于匹配一类具有相同特征的文本

1、匹配单个字符的元字符

.      匹配任意单个字符 

[root@localhost ~]# grep "r..t" /etc/passwd 
[root@localhost ~]# grep "a.k" /tmp/file01  
[ab]		或者a, 或者b 

[root@localhost ~]# grep "a[cH]k" /tmp/file01 
[root@localhost ~]# grep "a[8?]k" /tmp/file01 

[a-z]		任意单个小写字母

[root@localhost ~]# grep "a[a-z]k" /tmp/file01 

[A-Z]		任意单个大写字母 

[root@localhost ~]# grep "a[A-Z]k" /tmp/file01

[a-zA-Z]	任意单个字母  

[root@localhost ~]# grep "a[a-zA-Z]k" /tmp/file01

[0-9]		任意单个数字 

[root@localhost ~]# grep "a[0-9]k" /tmp/file01

[a-zA-Z0-9]

[root@localhost ~]# grep "a[a-zA-Z0-9]k" /tmp/file01 



[^a-z]		对小写字母进行取反

[root@localhost ~]# grep "a[^a-z]k" /tmp/file01
[[:space:]]		任意单个空白字符 , 空格、tab

[root@localhost ~]# grep "a[[:space:]]k" /tmp/file01

2、匹配字符出现的次数

* 	前一个字符连续出现任意次 

	.*   任意长度任意字符 

[root@localhost ~]# grep "ak*" /tmp/file01


\+   前一个字符至少出现1次 
	ab+    ab  abb  abbbbb 

[root@localhost ~]# grep "ak\+" /tmp/file01 
[root@localhost ~]# grep "a[0-9]\+" /tmp/file01


\?	前一个字符出现0次或者1次,可有可无
[root@localhost ~]# grep "ak\?" /tmp/file01  


\{3\}     前一个字符精确出现3次
	\{2,5\}		至少2次,最多5次
	\{2,\}		至少2次
[root@localhost ~]# grep "ak\{4\}" /tmp/file01


分组 \(ab\)
[root@localhost ~]# grep "\(ab\)\{2,\}" /usr/share/dict/words
[root@localhost ~]# grep "z[a-z]\+y" /usr/share/dict/words 

3、匹配字符出现位置的元字符

^string      以string开头的内容

[root@localhost ~]# grep "^root" /etc/passwd 
[root@localhost ~]# grep "^[sd]" /etc/passwd
[root@localhost ~]# grep "^[^sd]" /etc/passwd

string$      以string结尾的内容 

[root@localhost ~]# grep "sh$" /etc/passwd

^$    空行
[root@localhost ~]# grep "^$" /etc/fstab | wc -l 
[root@localhost ~]# grep "^$" /tmp/file01 | wc -l

正则应用案例

[root@localhost ~]# grep "a\.k" /tmp/file01  



匹配邮箱正则表达式

[root@localhost ~]# grep "[a-zA-Z0-9]\+@[0-9a-z]\+\.[a-z]\+" /tmp/file02

[root@localhost ~]# grep "[a-zA-Z0-9]\+@[0-9a-z]\+\.[a-z]\+\(\.[a-z]\+\)\?" /tmp/file02



匹配IP地址的正则

[root@localhost ~]# grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" /tmp/file03

三、常用选项

1、-o 只显示符合正则表达式的数据

[root@localhost ~]# grep -o "r..t" /etc/passwd

2、-i 忽略大小写

[root@localhost ~]# grep -i "^b" /tmp/file01

3、-v 反向过滤

[root@localhost ~]# grep -v "^#" /etc/fstab

4、-e 条件1 -e 条件2

[root@localhost ~]# grep -e "^#" -e "^/" /etc/fstab 

[root@localhost ~]# grep -e "^#" -e "0$" /etc/fstab

5、-E 支持扩展正则表达式

[root@localhost ~]# grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" /tmp/file03

二、查找文件

# find  目录名称  搜索条件 [-exec  命令  \;]

1、搜索条件

1) 按名称查找 -name

[root@localhost ~]# find /etc/ -name "*.conf"

2)按文件大小查找 -size

[root@localhost ~]# find /etc/ -size +1M 

[root@localhost ~]# find /etc/ -size -1M 

[root@localhost ~]# find /etc/ -size +400k 

3)按文件修改时间查找 -mtime

[root@localhost ~]# find /etc/ -mtime -3 		//3天内

[root@localhost ~]# find /etc/ -mtime +3 	  //3天前

4) 按文件创建时间查找 -ctime

[root@localhost ~]# find /etc/ -ctime -7

[root@localhost ~]# find /etc/ -ctime +7

5) 指定搜索目录层次 -maxdepth 2

[root@localhost ~]# find /a/ -maxdepth 2 -name "*.txt" 
[root@localhost ~]# find /etc/ -maxdepth 2 -name "*.conf" 

6)多条件查找

and  并且 

[root@localhost ~]# find /etc/ -name "*.conf" -a -size +50k


or  或者 

[root@localhost ~]# find /opt/testdir/ -name "*.txt" -o -name "*.html"

[root@localhost ~]# find /etc/ \( -name "*.conf" -o -name "*config*" \) -a -size +10k 

2、对查找的文件执行处理操作

[root@localhost ~]# find /opt/testdir/ -name "*.jpg" -exec rm -rf {} \;   

[root@localhost ~]# find /etc/ -size +2M -exec ls -lh {} \;

[root@localhost ~]# find /etc/ -size +2M -exec cp {} /tmp/ \;

三、去重

# uniq 文件名称  

[root@localhost ~]# uniq /tmp/file04

四、排序

# sort [选项]  文件名称

[root@localhost ~]# sort -n -k 2 -t ":" -r /tmp/b.txt

	-n    按数字处理 
	-k 2  按每行的第2列的数据进行排序, 默认使用空白字符分割每一行
	-t	  用于指定分割符 
	-r    降序、默认是升序 
	

[root@localhost ~]# sort /tmp/file04 | uniq 

[root@localhost ~]# du -a /etc/ | sort -n -k 1 

[root@localhost ~]# du -a /etc/ | sort -n -k 1 -r | less

posted @ 2023-03-25 10:29  nhxuan  阅读(105)  评论(0)    收藏  举报