您的位置:首页 > 运维架构 > Shell

常用shell脚本

2013-03-31 14:44 113 查看
1.按照内容分组

[root@localhost ~]# cat qq.tel

12334:13580226308

12334:13580126308

123334:13580226308

12099879:13810226308

12334:13810226306

12334:13510026308

12099879:13513263085

12099879:13760789764

12099879:13925067890

要求:按照第一列相同的进行分组

cat qq.tel|sort|awk -F : '{ if(tmp !=$1) {tmp=$1;print"["tmp"]";} print $2;}';

2.查询file里面空行的所在行号

grep -n ^$ file|awk -F : '{print $1}';

3.查询ip地址和掩码,并以ip/mask格式输出

ifconfig eth0 |grep "inet addr:" |awk '{print $2":"$4}' |awk -F : '{print $2"/"$4}';

方法2:ifconfig eth0|grep 'inet addr'|awk -F '[ :]' '{print $13}' 空格或者冒号作为分隔符。

方法3:[root@localhost ~]# ifconfig eth0 |awk -F '[ :]+' 'NR==2 {print $4}'

192.168.1.104

awk -F 后面跟分隔符‘[空格:]+’,其中[空格:]多分隔符写法,意思是以空格或冒号做分隔,后面的"+"号是正则表达式,意思是匹配前面空格或冒号,两者之一的1个或1个以上

匹配结果就是前面连续的空格为一列,inet第二列,addr第三列

[root@localhost ~]# ifconfig eth0 |awk NR==2;

inet addr:192.168.1.104 Bcast:192.168.1.255 Mask:255.255.255.0

[root@localhost ~]# ifconfig eth0 |awk -F '[ :]+' 'NR==2 {print $1}'

[root@localhost ~]# ifconfig eth0 |awk -F '[ :]+' 'NR==2 {print $2}'

inet

[root@localhost ~]# ifconfig eth0 |awk -F '[ :]+' 'NR==2 {print $3}'

addr
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: