sed(常用法)
参数:
-e 直接在指令列模式上进行sed的动作编辑(一条指令可以不加-e)
-i 直接编辑文件(重定向了)
命令(配合正则表达式使用 ):
-a 进行追加的
-d 进行删除
-s进行查找并替换
[root@master xiaosi]# ifconfig | sed -e '1,3s/192/XXX/g'
eth3 Link encap:Ethernet HWaddr 00:0C:29:4F:62:78
inet addr:XXX.168.71.100 Bcast:XXX.168.71.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4f:6278/64 Scope:Link
[root@master xiaosi]# ifconfig | sed -e '1,3s/192/XXX/g' -e '4,6d' #找1-3行的192皇换成XXX 并执行把4-6行删除
eth3 Link encap:Ethernet HWaddr 00:0C:29:4F:62:78
inet addr:XXX.168.71.100 Bcast:XXX.168.71.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4f:6278/64 Scope:Link
collisions:0 txqueuelen:1000
RX bytes:461584 (450.7 KiB) TX bytes:354255 (345.9 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:480 (480.0 b) TX bytes:480 (480.0 b)
在1-3行后一行加XXXX(-a)
[root@master xiaosi]# ifconfig | sed '1,3axiaosi'
eth3 Link encap:Ethernet HWaddr 00:0C:29:4F:62:78
xiaosi
inet addr:192.168.71.100 Bcast:192.168.71.255 Mask:255.255.255.0
xiaosi
inet6 addr: fe80::20c:29ff:fe4f:6278/64 Scope:Link
xiaosi
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4226 errors:0 dropped:0 overruns:0 frame:0
TX packets:2582 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:479584 (468.3 KiB) TX bytes:369971 (361.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:480 (480.0 b) TX bytes:480 (480.0 b)
把命令从Link到Up的部分,其中的line部分换成XXX
[root@master xiaosi]# ifconfig | sed '/Link/,/UP/s/line/XXXX/g'
eth3 Link encap:Ethernet HWaddr 00:0C:29:4F:62:78
inet addr:192.168.71.100 Bcast:192.168.71.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4f:6278/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4501 errors:0 dropped:0 overruns:0 frame:0
TX packets:2741 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:509857 (497.9 KiB) TX bytes:394713 (385.4 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:480 (480.0 b) TX bytes:480 (480.0 b)
cut
- -d 以什么为分隔符
- -f取第几列
- -c指定具体字符
[root@master xiaosi]# ifconfig |grep Bcast | cut -d: -f 1 |cut -c 1
[root@master xiaosi]# ifconfig |grep Bcast | cut -d: -f 1
inet addr
[root@master xiaosi]# ifconfig |grep Bcast | cut -d: -f 2
192.168.71.100 Bcast
[root@master xiaosi]# ifconfig |grep Bcast | cut -d: -f 2 | cut -c 2-8
92.168.
awk(待补)
sort
- -n 依照数值的大小排序
- -r 以相反的顺序来牌序
- -t 设置以什么来排(分隔符)
- -k 指定需要排序的列
[root@master xiaosi]# cat passwd | grep ^a | sort -n
abrt:x:173:173::/etc/abrt:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
[root@master xiaosi]# cat passwd | grep ^a | sort -t : -k 3
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
[root@master xiaosi]# cat passwd | grep ^a | sort -t : -k 3 -n
adm:x:3:4:adm:/var/adm:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin