常用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
[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
相关文章推荐
- 【转】shell脚本处理字符串的常用方法
- 8个DBA最常用的监控Oracle数据库的常用shell脚本
- Linux常用shell脚本珍藏
- 自己常用的一些shell脚本分享
- shell常用脚本
- 常用的主机监控的 Shell 脚本
- 我常用的主机监控Shell脚本
- Linux下一些常用的Shell脚本整理
- 9个实战及面试常用Shell脚本编写
- shell 脚本 常用总结
- Shell脚本中常用的循环语句
- Linux运维常用的Shell脚本(带注解)
- Linux常用shell脚本
- Linux中常用的Shell备份脚本三则
- 常用的shell脚本
- shell脚本中常用的命令:wget、curl、ss、lsof、nmap、nc、netstat、telnet
- spring boot 以jar的方式启动常用shell脚本
- 常用shell脚本片段
- linux学习笔记(20) - shell脚本编写常用判断命令
- shell脚本FAQ与常用数据库服务器监控脚本示例