grep 命令:过滤文件内容
wc 命令:统计内容数量
| 管道符概念和应用
1、grep命令,从文件中通过关键字过滤文件行
语法:grep [-n] 关键字 文件路径
grep [-n] 关键字 文件路径
选项-n, 可选,表示在结果中显示匹配的行的行号;
参数, 关键字, 必填, 表示过滤的关键字,带有空格或其它特殊符号,建议使用""将关键字包围起来;
参数, 文件路径, 必填, 表示要过滤内容的文件路径,可作为内容输入端口;
[shui@xiaored ~]$ grep -n root demo.txt
2:lrwxrwxrwx. 1 root root 7 4月 24 2021 bin -> usr/bin
3:dr-xr-xr-x. 5 root root 4096 6月 13 15:56 boot
4:drwxr-xr-x. 20 root root 3180 8月 21 11:28 dev
5:drwxr-xr-x. 92 root root 8192 8月 21 15:31 etc
6:drwxr-xr-x. 4 root root 33 8月 14 15:35 home
7:lrwxrwxrwx. 1 root root 7 4月 24 2021 lib -> usr/lib
8:lrwxrwxrwx. 1 root root 9 4月 24 2021 lib64 -> usr/lib64
9:drwxr-xr-x. 2 root root 6 4月 11 2018 media
10:drwxr-xr-x. 3 root root 18 4月 24 2021 mnt
11:drwxr-xr-x. 4 root root 48 8月 8 16:14 opt
12:dr-xr-xr-x. 127 root root 0 8月 21 11:28 proc
13:dr-xr-x---. 3 root root 163 4月 25 2021 root
14:drwxr-xr-x. 29 root root 820 8月 21 11:28 run
15:lrwxrwxrwx. 1 root root 8 4月 24 2021 sbin -> usr/sbin
16:drwxr-xr-x. 2 root root 6 4月 11 2018 srv
17:dr-xr-xr-x. 13 root root 0 8月 21 11:28 sys
18:drwxrwxrwt. 13 root root 4096 8月 21 15:33 tmp
19:drwxr-xr-x. 13 root root 155 4月 24 2021 usr
20:drwxr-xr-x. 19 root root 267 4月 24 2021 var
[shui@xiaored ~]$ grep 11 demo.txt
drwxr-xr-x. 20 root root 3180 8月 21 11:28 dev
drwxr-xr-x. 2 root root 6 4月 11 2018 media
dr-xr-xr-x. 127 root root 0 8月 21 11:28 proc
drwxr-xr-x. 29 root root 820 8月 21 11:28 run
drwxr-xr-x. 2 root root 6 4月 11 2018 srv
dr-xr-xr-x. 13 root root 0 8月 21 11:28 sys
[shui@xiaored ~]$
2、wc命令 - 数量统计
语法: wc [-c -m -l -w] 文件路径
wc [-c -m -l -w] 文件路径
选项, -c, 统计bytes数量
选项, -m, 统计字节数量
选项, -l, 统计行数
选项, -w, 统计单词数量
参数, 文件路径, 被统计的文件, 可作为内容输入端口
不带选项默认统计:行数、单词数、字节数;
-c 字节数、 -m 字符数、 -l 行数、 -w 单词数
参数,被统计的文件路径,可作为管道符的输入
3、| 管道符
管道符的含义是:将管道符左边命令的结果,作为右边命令的输入
cat itheima.txt 的输出结果(文件内容)
作为右边grep 命令的输入(被过滤文件)
[shui@xiaored ~]$ ll
总用量 4
-rw-rw-r--. 1 shui shui 110 8月 21 15:36 demo.txt
drwxrwxr-x. 2 shui shui 6 8月 21 15:34 newfile
[shui@xiaored ~]$ cat demo.txt | grep -n cd
1:cd
7:cd /home
过滤结果
[shui@xiaored ~]$ ll
总用量 4
-rw-rw-r--. 1 shui shui 110 8月 21 15:36 demo.txt
drwxrwxr-x. 2 shui shui 6 8月 21 15:34 newfile
[shui@xiaored ~]$ ll | grep -n new
3:drwxrwxr-x. 2 shui shui 6 8月 21 15:34 newfile
[shui@xiaored ~]$
嵌套使用
[shui@xiaored ~]$ cat demo.txt | grep -n root | grep -n 13
2:3:dr-xr-xr-x. 5 root root 4096 6月 13 15:56 boot
12:13:dr-xr-x---. 3 root root 163 4月 25 2021 root
16:17:dr-xr-xr-x. 13 root root 0 8月 21 11:28 sys
17:18:drwxrwxrwt. 13 root root 4096 8月 21 15:33 tmp
18:19:drwxr-xr-x. 13 root root 155 4月 24 2021 usr
[shui@xiaored ~]$