grep命令

grep    关键字符    文件|目录   ##在文件或目录中查找含有关键字的行

grep    -i          ##忽略大小写
        -n          ##显示关键字所在行
        -c          ##显示过滤结果的个数
        -v          ##反向过滤
        -E  "关键字1|关键字2" ##过滤多个关键字
        -r  目录      ##在目录中查找含有关键字的文件
注意: ^关键字            ##以关键字开头
    关键字$           ##以关键字结尾

接下来是一些具体使用
先准备一个文件来做测试,文件为/aaa/file1

[root@localhost ~]# cat /aaa/file1  ##查看/aaa/file1文件内容
test:root:sssd
TEST:sssd:root
TEST:sssd:root
sssd:test:test
root:test:sssd
root:root:sssd
[root@localhost ~]# grep -i test /aaa/file1   ##将 /aaa/file1文件内容里含有test的行不分大小写找出来
test:root:sssd
TEST:sssd:root
TEST:sssd:root
sssd:test:test
root:test:sssd
[root@localhost ~]# grep -n test /aaa/file1  ##将 /aaa/file1文件内容里含有小写的test的行以及行号找出来
1:test:root:sssd
4:sssd:test:test
5:root:test:sssd
[root@localhost ~]# grep -n -c test /aaa/file1  ##将 /aaa/file1文件内容里含有小写的test的行的行数输出
3
[root@localhost ~]# grep -n -v test /aaa/file1  ##将 /aaa/file1文件内容里不含有小写的test的行以及行号找出来
2:TEST:sssd:root
3:TEST:sssd:root
6:root:root:sssd
[root@localhost ~]# grep -n -r test /aaa/   ##将目录/aaa/下的含有test的行找出来
/aaa/file1:1:test:root:sssd
/aaa/file1:4:sssd:test:test
/aaa/file1:5:root:test:sssd
[root@localhost ~]# grep -E "^test" /aaa/file1  ##将/aaa/file1文件内容的每行开头是test的行找出来
test:root:sssd
[root@localhost ~]# grep -E "test$" /aaa/file1    ##将/aaa/file1文件内容的每行末尾是test的行找出来
sssd:test:test
[root@localhost ~]# grep -E -v "^test|test$" /aaa/file1 ##将/aaa/file1文件内容每行开头和结尾都不是test的找出来
TEST:sssd:root
TEST:sssd:root
root:test:sssd
root:root:sssd
[root@localhost ~]# grep -E "^test|test$" /aaa/file1 ##将/aaa/file1文件内容每行开头或结尾是test的找出来
test:root:sssd
sssd:test:test

cut命令

cut 用于 “剪切 ”文件中的文本字段或列并将
其显示到标准输出

cut                 ##截取字符
cut -b   --bytes=LIST        select only these bytes 列表只选择这些字节
cut -c   --characters=LIST   select only these characters 列表只选择这些字符
cut -d  分隔符         ##指定分隔符
cut -f 1,7          ##显示指定的列
cut -c 1-4          ##显示指定的字符

接下来做一些cut命令的使用
使用/aaa/file1文件内容做测试,文件内容如下

[root@localhost ~]# cat /aaa/file1
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
tcpdump:x:72:72::/:/sbin/nologin
[root@localhost ~]# cut -b 5 /aaa/file1  ##将第五列找出来
:
x
o
x
:
:
u
[root@localhost ~]# cut -c 1-5 /aaa/file1  ##将第一列到第五列找出来
root:
bin:x
daemo
adm:x
lp:x:
sync:
tcpdu
[root@localhost ~]# cut -d : -f 1,3 /aaa/file1  ##指定分隔符为: 将第一列到第三列找出来
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
tcpdump:72

小练习

[root@localhost ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.20  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fd0d:8b28:1659::ce8  prefixlen 128  scopeid 0x0<global>
        inet6 fd0d:8b28:1659:0:5054:ff:fe00:300b  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::5054:ff:fe00:300b  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:00:30:0b  txqueuelen 1000  (Ethernet)
        RX packets 9447  bytes 4598349 (4.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14879  bytes 1327420 (1.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
如何将172.25.254.20找出来,只要这个
[root@localhost ~]# ifconfig eth0 | grep inet | grep inet6 -v |awk -F " " '{printf $2}'
172.25.254.20
[root@localhost ~]#
关于awk命令
awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。
awk [-F  field-separator]  'commands'  input-file(s)
其中,commands 是真正awk命令,[-F域分隔符]是可选的。 input-file(s) 是待处理的文件。
在awk中,文件的每一行中,由域分隔符分开的每一项称为一个域。通常,在不指名-F域分隔符的情况下,默认的域分隔符是空格。