过滤字段,查找替换(学习笔记)

来源:互联网 发布:知世故而不世故的图片 编辑:程序博客网 时间:2024/05/24 23:15

过滤字段

Sed  做行操作

Cut  取字段

Awk 做列操作

我们以ifconfig命令查看的信息为基础进行实验操作。

[root@localhost ~]# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:0C:29:23:AB:9B  

          inet addr:10.15.62.115  Bcast:10.15.62.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe23:ab9b/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:72662 errors:0 dropped:0 overruns:0 frame:0

          TX packets:73 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:35091571 (33.4 MiB)  TX bytes:11507 (11.2 KiB)

 

lo        Link encap:Local Loopback  

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:8 errors:0 dropped:0 overruns:0 frame:0

          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0 

          RX bytes:560 (560.0 b)  TX bytes:560 (560.0 b)

要求一:

显示出ifconfig第一行中以空格为分隔符的第二列内容

[root@localhost ~]# ifconfig | sed -n '1p' | awk -F " " ' {print $2} '

Link

要求二:

ifconfig文件中过滤出eth0这一行,然后以冒号为分隔符取第三段内容

[root@localhost ~]# ifconfig | grep eth0 | awk -F ":" ' {print $3} '

0C

[root@localhost ~]# ifconfig | grep eth0 | cut -d ":" -f3

0C

要求三:

Ifconfig去除eth0这一行内容,显示余下的所有内容

[root@localhost ~]# ifconfig | grep -v eth0

          inet addr:10.15.62.115  Bcast:10.15.62.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe23:ab9b/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:139927 errors:0 dropped:0 overruns:0 frame:0

          TX packets:328 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:67824283 (64.6 MiB)  TX bytes:39241 (38.3 KiB)

 

lo        Link encap:Local Loopback  

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:8 errors:0 dropped:0 overruns:0 frame:0

          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0 

          RX bytes:560 (560.0 b)  TX bytes:560 (560.0 b)

要求四:

只显示ifconfig中第一行内容

[root@localhost ~]# ifconfig | sed -n "1p"

eth0      Link encap:Ethernet  HWaddr 00:0C:29:23:AB:9B 

要求五:

显示ifconfig1--4行的内容

[root@localhost ~]# ifconfig | sed -n "1,4p"

eth0      Link encap:Ethernet  HWaddr 00:0C:29:23:AB:9B  

          inet addr:10.15.62.115  Bcast:10.15.62.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe23:ab9b/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

要求六:

查找替换字段(将第一行中的eth0替换成eth1,并筛选显示修改过的第一行内容

[root@localhost ~]# ifconfig | sed '1 s/eth0/eth1/g' | sed -n "1p"

eth1      Link encap:Ethernet  HWaddr 00:0C:29:23:AB:9B  

要求七:

查找替换字段(将所有行中的eth0替换成eth1,并筛选显示修改过的第一行内容

[root@localhost ~]# ifconfig | sed '1,$s/eth0/eth1/g' | sed -n '1p'

eth1      Link encap:Ethernet  HWaddr 00:0C:29:23:AB:9B  

查找命令

Find 目录 -name 要查找的文件

Find 目录 -name 要查找的文件 -exec 查找命令{}\

find [-path……] -options [-print -exec -ok]

Path:要查找的目录路径,~表示$HOME目录,表示当前目录, / 根目录

Print:表示将结果输出到标准输出;

Exec:对匹配的文件执行该参数给出的shell命令;

比如:形式为command {} \;,注意{}\;之间有空格

Ok:与exec作用相同,执行命令之前,会给出确认提示;

Options:表示以哪种格式查找

比如:

-name 按照名字查找;

-perm:安装权限查找;

-user:文件属主查找;

-prune:不再当前指定的目录下查找;

-group:文件所属组查找;

-type:文件类型查找;

-size:文件大小查找;

 

例:

以文件类型查找:

Find 目录 -type d -name 目录名字

如:

[root@localhost home]# find /home -type d -name redisma

/home/redisma

 

Find 目录 -type f -name 文件名字

[root@localhost home]# find /home -type f -name aa.sh

/home/aa.sh

 

删除前提示确认 注意{}与\;之间有空格 

[root@localhost home]# find /home -type d -name "redis*" -ok rm {} \;

< rm ... /home/redissa > ? n

< rm ... /home/redisma > ? N

N表示no,不删除,y表示yes,确认删除

 

 

查到需要的内容后,将所需要的信息移动到另外一个目录下:

[root@localhost home]# find /home -type d -name "redis*" -exec cp {} /opt \;

cp: omitting directory `/home/redissa'

cp: omitting directory `/home/redisma'

查找到redis开头的信息之后,复制到/opt目录下

 

0 0
原创粉丝点击