手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

sehll脚本将几行输出合并成一行显示

[日期:2013-11-02] 来源:Linux社区  作者:avilifans [字体:大 中 小]

shell脚本实现将文件中的几行输出合并成一行显示

现在有一个文件aa.txt ,其实现格式如下,而我想实现的结果是192.168.1.17 down

[root@localhost shell]# cat aa.txt
192.168.1.17
down
192.168.1.103
open
192.168.1.221
open
192.168.1.104
down
192.168.1.175
down
192.168.1.112
open
192.168.1.11
down


[root@localhost shell]# awk '{if (NR%2==0){print $0} else {printf"%s ",$0}}' aa.txt
192.168.1.17 down
192.168.1.103 open
192.168.1.221 open
192.168.1.104 down
192.168.1.175 down
192.168.1.112 open
192.168.1.11 down


或者

[root@localhost shell]# cat aa.txt | awk '{if(NR%2!=0)ORS=" ";else ORS="\n";print}'
192.168.1.17 down
192.168.1.103 open
192.168.1.221 open
192.168.1.104 down
192.168.1.175 down
192.168.1.112 open
192.168.1.11 down

但是如果aa.txt 的内容如果换种形式

#################################################


搞开发的同事让我把他写个脚本可以实现ipset.sh  eth0 192.168.1.1 255.255.255.0 192.168.254.254来配置某个网卡的信息

#!/bin/bash
hwaddr=`ifconfig |grep HWaddr |awk '{print $5}'`
echo "DEVICE=$1
HWADDR=$hwaddr
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=$2
NETMASK=$3
GATEWAY=$4" > /etc/sysconfig/network-scripts/ifcfg-$1
service network restart

推荐阅读:

Linux Shell脚本语言与数学表达式  http://www.linuxidc.com/Linux/2013-09/90632.htm

Shell 循环语句用法小结 http://www.linuxidc.com/Linux/2012-02/54474.htm

Linux下Shell编程——awk编程 http://www.linuxidc.com/Linux/2013-06/85527.htm

Linux下Shell编程——sed命令基本用法 http://www.linuxidc.com/Linux/2013-06/85526.htm

Linux下Shell编程——grep命令的基本运用 http://www.linuxidc.com/Linux/2013-06/85525.htm

Linux下Shell编程——正则表达式基础与扩展 http://www.linuxidc.com/Linux/2013-06/85523.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款