免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2535 | 回复: 11
打印 上一主题 下一主题

[文本处理] shell 替换字符串 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-03-10 11:49 |只看该作者 |倒序浏览
本帖最后由 biubiuu 于 2015-03-10 11:50 编辑

我要更换网卡0的mac和ip,下面是/etc/network/interfaces 文件的一部分
auto eth0
iface eth0 inet static
        pre-up ifconfig eth0 hw ether 84:7E:40:EF6:C6
        address 192.168.2.88
        netmask 255.255.255.0
        network 192.168.1.0
        gateway 192.168.1.1

我是这么做的
ip=192.168.1.1
mac=01:01:01:01:01:01

sed -i -e "0,/^address/s/.*\(address\).*/ address $ip/" $file_path
sed -i -e "0,/^pre-up/s/.*eth0.*/ pre-up ifconfig eth0 hw ether $hwadd/" $file_path

发现把后面网卡1的address也改了,之前含有eth0的行改成 pre-up ifconfig eth0 hw ether 01:01:01:01:01:01了
求各位帮帮忙,应该怎么改才对

求职 : 机器学习
论坛徽章:
79
2015年亚洲杯纪念徽章
日期:2015-05-06 19:18:572015七夕节徽章
日期:2015-08-21 11:06:172015亚冠之阿尔纳斯尔
日期:2015-09-07 09:30:232015亚冠之萨济拖拉机
日期:2015-10-21 08:26:3915-16赛季CBA联赛之浙江
日期:2015-12-30 09:59:1815-16赛季CBA联赛之浙江
日期:2016-01-10 12:35:21技术图书徽章
日期:2016-01-15 11:07:2015-16赛季CBA联赛之新疆
日期:2016-02-24 13:46:0215-16赛季CBA联赛之吉林
日期:2016-06-26 01:07:172015-2016NBA季后赛纪念章
日期:2016-06-28 17:44:45黑曼巴
日期:2016-06-28 17:44:4515-16赛季CBA联赛之浙江
日期:2017-07-18 13:41:54
2 [报告]
发表于 2015-03-10 12:25 |只看该作者
贴一个完整的文本啊回复 1# biubiuu


   

论坛徽章:
28
2015年迎新春徽章
日期:2015-03-04 10:16:53午马
日期:2015-03-20 17:11:07亥猪
日期:2015-03-20 17:11:23戌狗
日期:2015-03-20 17:11:33酉鸡
日期:2015-03-20 17:11:47申猴
日期:2015-03-20 17:11:58未羊
日期:2015-03-20 17:12:10子鼠
日期:2015-03-20 17:12:23丑牛
日期:2015-03-20 17:12:40卯兔
日期:2015-03-20 17:12:51辰龙
日期:2015-03-20 17:13:00巳蛇
日期:2015-03-20 17:13:12
3 [报告]
发表于 2015-03-10 12:28 |只看该作者
本帖最后由 我是一隻羊 于 2015-03-10 12:51 编辑
  1. sed -ri '/iface eth0/{N;N;s/(ether )[^\n]*/\1'$mac'/;s/(address )[^\n]*/\1'$ip'/}' inputfile
复制代码
  1. awk '/iface/{a=$2}{if(a=="eth0"){sub(/address.*/,"address '$ip'");sub(/ether.*/,"ether '$mac'")}}1' inputfile
复制代码

求职 : 机器学习
论坛徽章:
79
2015年亚洲杯纪念徽章
日期:2015-05-06 19:18:572015七夕节徽章
日期:2015-08-21 11:06:172015亚冠之阿尔纳斯尔
日期:2015-09-07 09:30:232015亚冠之萨济拖拉机
日期:2015-10-21 08:26:3915-16赛季CBA联赛之浙江
日期:2015-12-30 09:59:1815-16赛季CBA联赛之浙江
日期:2016-01-10 12:35:21技术图书徽章
日期:2016-01-15 11:07:2015-16赛季CBA联赛之新疆
日期:2016-02-24 13:46:0215-16赛季CBA联赛之吉林
日期:2016-06-26 01:07:172015-2016NBA季后赛纪念章
日期:2016-06-28 17:44:45黑曼巴
日期:2016-06-28 17:44:4515-16赛季CBA联赛之浙江
日期:2017-07-18 13:41:54
4 [报告]
发表于 2015-03-10 12:59 |只看该作者
本帖最后由 zsszss0000 于 2015-03-10 13:19 编辑
  1. cat file | awk -vMAC="11:22:33:44:55:66" -vIP="11.22.33.44" '{if($0~"auto eth0"){flag=1}else if($0~"auto eth1"){flag=0};if(flag==1){if($0~"ifconfig eth0")$7=MAC;if($0~"address")$2=IP;}}1'
复制代码
  1. cat file | awk -vMAC="11:22:33:44:55:66" -vIP="11.22.33.44" '{if($0~"auto eth0"){flag=1}else if($0~"auto eth[1-9]"){flag=0};if(flag==1){if($0~"ifconfig eth0")$7=MAC;if($0~"address")$2=IP;}}1'
复制代码

论坛徽章:
0
5 [报告]
发表于 2015-03-10 14:15 |只看该作者
回复 2# zsszss0000
# /etc/network/interfaces -- configuration file for ifup(, ifdown(

# The loopback interface
auto lo
iface lo inet loopback


# Wireless interfaces
#
# Example of an unencrypted (no WEP or WPA) wireless connection
# that connects to any available access point:
#
iface wlan0 inet dhcp
        wireless_mode managed
        wireless_essid any

iface tiwlan0 inet dhcp
        wireless_mode managed
        wireless_essid any
#
#
# Same as above but locked to a specific access point:
#
#iface wlan0 inet dhcp
#       wireless_mode managed
#       wireless-essid some-essid
#
# A WEP encrypted connection locked to a specific access point:
#
#iface wlan0 inet dhcp
#       wireless-essid some-essid
#       wireless-key s:My-PlainText-Password
#       wireless-mode managed
#
# A WPA1 or WPA2 encrypted connection locked to a specific access point.
# This is the best option for non-roaming, single-network usage.
# Note that your card may require a firmware update to use WPA.
# Some distributions install a temporary volatile firmware update on ifup.
#
#iface wlan0 inet dhcp
#       wpa-essid some-essid
#       wpa-psk My-PlainText-Password                                          
#
# A WPA1 or WPA2 encrypted connection using an external configuration file
# for wpa-supplicant. This is the way to go if you need to configure multiple
# networks with different keys and / or WPA settings and roaming support.
#
# iface wlan0 inet dhcp
#    wpa-conf /etc/wpa_supplicant.conf
#    wpa-driver hostap



iface atml0 inet dhcp

# Wired or wireless interfaces
auto eth0
iface eth0 inet static
        pre-up ifconfig eth0 hw ether 84:7E:40:EF6:C6
        address 192.168.2.88
        netmask 255.255.255.0
        network 192.168.1.0
        gateway 192.168.1.1
#iface eth0 inet dhcp
#        pre-up /bin/grep -v -e "ip=[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+" /proc/cmdline > /dev/null

#iface eth1 inet dhcp
auto eth1
iface eth1 inet static
        address 192.168.1.89
        netmask 255.255.255.0
        network 192.168.1.0
        gateway 192.168.1.1

# Ethernet/RNDIS gadget (g_ether)
# ... or on host side, usbnet and random hwaddr
iface usb0 inet dhcp

# Zaurus 2.4 Lineo net_fd; obsolete
iface usbd0 inet static
        address 192.168.129.201
        netmask 255.255.255.0
        network 192.168.129.0
        gateway 192.168.129.200

# iPAQ 2.4 mach-sa1100/usb-eth
# (192.168.0.202 is the iPAQ's IP, 192.168.0.200 is the host's IP)
iface usbf inet static
        address 192.168.0.202
        netmask 255.255.255.0
        network 192.168.0.0
        gateway 192.168.0.200

# Bluetooth networking
iface bnep0 inet dhcp


   

求职 : 机器学习
论坛徽章:
79
2015年亚洲杯纪念徽章
日期:2015-05-06 19:18:572015七夕节徽章
日期:2015-08-21 11:06:172015亚冠之阿尔纳斯尔
日期:2015-09-07 09:30:232015亚冠之萨济拖拉机
日期:2015-10-21 08:26:3915-16赛季CBA联赛之浙江
日期:2015-12-30 09:59:1815-16赛季CBA联赛之浙江
日期:2016-01-10 12:35:21技术图书徽章
日期:2016-01-15 11:07:2015-16赛季CBA联赛之新疆
日期:2016-02-24 13:46:0215-16赛季CBA联赛之吉林
日期:2016-06-26 01:07:172015-2016NBA季后赛纪念章
日期:2016-06-28 17:44:45黑曼巴
日期:2016-06-28 17:44:4515-16赛季CBA联赛之浙江
日期:2017-07-18 13:41:54
6 [报告]
发表于 2015-03-10 14:35 |只看该作者
楼上的几种方法应该没有问题回复 5# biubiuu


   

论坛徽章:
0
7 [报告]
发表于 2015-03-10 15:26 |只看该作者
回复 6# zsszss0000
在这先谢谢大家了,我继续研究

   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
8 [报告]
发表于 2015-03-10 16:12 |只看该作者
回复 1# biubiuu

$ awk -viface="eth0" -vip="$ip" -vmac="$mac" '/^[ \t]*#/||NF==0{print;next}/^[ \t]*iface/{seg=$2==iface?1:0}seg{if(/address/){$1="\t"$1;$2=ip};if(/pre-up/){$1="\t"$1;$NF=mac}}1' FILE > new

$ diff FILE new
59,60c59,60
<         pre-up ifconfig eth0 hw ether 84:7E:40:EF6:C6
<         address 192.168.2.88
---
>         pre-up ifconfig eth0 hw ether 01:01:01:01:01:01
>         address 192.168.1.1

   

论坛徽章:
8
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015小元宵徽章
日期:2015-03-06 15:58:18每日论坛发贴之星
日期:2015-06-08 22:20:00每日论坛发贴之星
日期:2015-06-08 22:20:00操作系统版块每日发帖之星
日期:2015-06-14 22:20:00数据库技术版块每日发帖之星
日期:2015-11-09 06:20:00数据库技术版块每日发帖之星
日期:2016-02-22 06:20:0015-16赛季CBA联赛之上海
日期:2017-01-01 23:58:53
9 [报告]
发表于 2015-03-10 22:40 |只看该作者
同意jason的观点

论坛徽章:
2
黑曼巴
日期:2017-09-18 16:16:1315-16赛季CBA联赛之广东
日期:2017-10-25 16:20:16
10 [报告]
发表于 2015-04-13 10:01 |只看该作者
is  master
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP