刚在linuxtone 见到一个案例,无奈无法回复,只好贴在这里,作为shell题目收集下来。
文本如下
- lease 10.127.1.254 {
- starts 1 2012/08/20 06:58:48;
- ends 3 2012/09/19 06:58:48;
- tstp 3 2012/09/19 06:58:48;
- binding state active;
- next binding state free;
- hardware ethernet 11:22:33:44:55:84;
- uid "\001x+\313C\270\204";
- client-hostname "rac-ABCDEF";
- }
- lease 10.127.1.253 {
- starts 1 2012/08/20 06:58:48;
- ends 3 2012/09/19 06:58:48;
- tstp 3 2012/09/19 06:58:48;
- binding state active;
- next binding state free;
- hardware ethernet 11:22:33:44:55:84;
- uid "\001x+\313C\270\204";
- client-hostname "rac-AAAAA";
- }
要求将IP和主机名过滤出来,格式【IP 主机名】
已经有大牛提供了办法,但是我还是说下自己的解决思路:
1、利用grep将IP和主机名过滤出来
2、合并行
3、打印
全过程
- $ grep 'lease\|hostname' file.txt
- lease 10.127.1.254 {
- client-hostname "rac-ABCDEF";
- lease 10.127.1.253 {
- client-hostname "rac-AAAAA";
- $ grep 'lease\|hostname' file.txt |awk '{print $2}'
- 10.127.1.254
- "rac-ABCDEF";
- 10.127.1.253
- "rac-AAAAA";
- $ grep 'lease\|hostname' txt |awk '{print $2}' |awk '{if (NR%2==0){print $0} else {printf"%s ",$0}}'
- 10.127.1.254 "rac-ABCDEF";
- 10.127.1.253 "rac-AAAAA";
用的工具不同,步骤可以省略一两步
- $ awk '/lease/||/client-hostname/{print $2}' new
- 10.127.1.254
- "rac-ABCDEF";
- 10.127.1.253
- "rac-AAAAA";
本文转自 紫色葡萄 51CTO博客,原文链接:http://blog.51cto.com/purplegrape/970871,如需转载请自行联系原作者