shell每日更新(5)

6月11号 晚上8点半

提取字段用:awk 与cut 如提取passwd中第五个字段。

[root@fsailing1 shell]# awk -F: '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
[root@fsailing1 shell]# cut -d: -f 5 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
解释下awk中print与printf的区别:没有分开全部按顺序写出来。

[root@fsailing1 ~]# awk -F: '{printf $5}' /etc/passwd
rootbindaemonadmlpsyncshutdownhaltmailnewsuucpoperatorgamesgopherFTP UserNobodyDistcacheNSCD Daemonvirtual console memory ownerApachePortmapper RPC userWebalizerdovecotSystem message busAvahi daemonRPC Service UserAnonymous NFS UserNamedPrivilege-separated SSHHAL daemonavahi-autoipdX Font ServerMySQL Server[root@fsaili

6月12号 下午2点

正则表达式,IP地址的匹配:使用grep的时候不太支持正则表达式,需要转义字符\匹配使用

[root@fsailing1 shell]# cat chen3.txt
192.168.110.1
10.5.110.238
192.184.110.112
1110.333.44.1111
[root@fsailing1 shell]# cat chen3.txt|egrep '([0-9]\{1,3\}\.)\{1,3\}([0-9])\{1,3\}'
[root@fsailing1 shell]# cat chen3.txt|grep '([0-9]\{1,3\}\.)\{1,3\}([0-9])\{1,3\}'
[root@fsailing1 shell]# cat chen3.txt|grep '\([0-9]\{1,3\}\.\)\{1,3\}\([0-9]\)\{1,3\}'
192.168.110.1
10.5.110.238
192.184.110.112
1110.333.44.1111
如果使用egrep就不同了,它是grep里面专门用来匹配正则表达式的。

[root@fsailing1 shell]# cat chen3.txt|egrep '([0-9]\.{1,3}){1,3}([0-9]){1,3}'
192.168.110.1
10.5.110.238
192.184.110.112
1110.333.44.1111






你可能感兴趣的:(shell每日更新(5))