免费注册 查看新帖 |

Chinaunix

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

[文本处理] sed能否用file1的内容替换掉file2特定行的内容 [复制链接]

论坛徽章:
2
亥猪
日期:2015-01-19 15:47:392015年亚洲杯之朝鲜
日期:2015-04-08 16:27:18
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-05-21 15:40 |只看该作者 |倒序浏览



如图
咋用sed让file1的字符串替换file2文件里的特定行的字符串,谢谢

论坛徽章:
2
2015年亚洲杯之伊拉克
日期:2015-04-13 10:55:1115-16赛季CBA联赛之山西
日期:2016-05-16 09:54:41
2 [报告]
发表于 2015-05-21 15:56 |只看该作者
没明白你说的是什么。。。。

论坛徽章:
2
亥猪
日期:2015-01-19 15:47:392015年亚洲杯之朝鲜
日期:2015-04-08 16:27:18
3 [报告]
发表于 2015-05-21 16:20 |只看该作者
就是用下面那张图里的内容替换上面那张图的特定的三行,address,netmask,gateway这三行回复 2# little_angel


   

论坛徽章:
2
亥猪
日期:2015-01-19 15:47:392015年亚洲杯之朝鲜
日期:2015-04-08 16:27:18
4 [报告]
发表于 2015-05-21 16:22 |只看该作者
  1.         address 192.168.1.123
  2.         netmask 255.255.255.0
  3.         gateway 192.168.1.1
复制代码
  1. # /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)

  2. # The loopback interface
  3. auto lo
  4. iface lo inet loopback


  5. # Wireless interfaces
  6. #
  7. # Example of an unencrypted (no WEP or WPA) wireless connection
  8. # that connects to any available access point:
  9. #
  10. iface wlan0 inet dhcp
  11.         wireless_mode managed
  12.         wireless_essid any
  13. #
  14. #
  15. # Same as above but locked to a specific access point:
  16. #
  17. #iface wlan0 inet dhcp  
  18. #        wireless_mode managed
  19. #        wireless-essid some-essid
  20. #
  21. # A WEP encrypted connection locked to a specific access point:
  22. #
  23. #iface wlan0 inet dhcp  
  24. #        wireless-essid some-essid
  25. #        wireless-key s:My-PlainText-Password
  26. #        wireless-mode managed                  
  27. #
  28. # A WPA1 or WPA2 encrypted connection locked to a specific access point.
  29. # This is the best option for non-roaming, single-network usage.
  30. # Note that your card may require a firmware update to use WPA.
  31. # Some distributions install a temporary volatile firmware update on ifup.
  32. #
  33. #iface wlan0 inet dhcp                                                   
  34. #        wpa-essid some-essid                                          
  35. #        wpa-psk My-PlainText-Password                                             
  36. #
  37. # A WPA1 or WPA2 encrypted connection using an external configuration file
  38. # for wpa-supplicant. This is the way to go if you need to configure multiple
  39. # networks with different keys and / or WPA settings and roaming support.
  40. #
  41. # iface wlan0 inet dhcp
  42. #    wpa-conf /etc/wpa_supplicant.conf
  43. #    wpa-driver hostap



  44. iface atml0 inet dhcp

  45. # Wired or wireless interfaces
  46. auto eth0
  47. iface eth0 inet static
  48.         address 192.168.1.22
  49.         netmask 255.255.255.0
  50.         gateway 192.168.1.1

  51. #iface eth0 inet dhcp
  52. iface eth1 inet dhcp

  53. # Ethernet/RNDIS gadget (g_ether)
  54. # ... or on host side, usbnet and random hwaddr
  55. iface usb0 inet static
  56.         address 192.168.0.202
  57.         netmask 255.255.255.0
  58.         network 192.168.0.0
  59.         gateway 192.168.0.200


  60. # Zaurus 2.4 Lineo net_fd; obsolete
  61. iface usbd0 inet static
  62.         address 192.168.129.201
  63.         netmask 255.255.255.0
  64.         network 192.168.129.0
  65.         gateway 192.168.129.200

  66. # iPAQ 2.4 mach-sa1100/usb-eth
  67. # (192.168.0.202 is the iPAQ's IP, 192.168.0.200 is the host's IP)
  68. iface usbf inet static
  69.         address 192.168.0.202
  70.         netmask 255.255.255.0
  71.         network 192.168.0.0
  72.         gateway 192.168.0.200

  73. # Bluetooth networking
  74. iface bnep0 inet dhcp
复制代码

论坛徽章:
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
5 [报告]
发表于 2015-05-21 17:06 |只看该作者
回复 4# 791605975

$ awk 'FNR==NR{a[$1]=$2;next}/^iface /{g=($2=="eth0")?1:0}g&&/^[^#]/{if(a[$1])sub($1".*$",$1" "a[$1])}1' file1 file2 > file3

$ cat file1
        address 192.168.1.123
        netmask 255.255.255.0
        gateway 192.168.1.1


$ grep -A3 '^iface eth0' file2
iface eth0 inet static
        address 192.168.1.22
        netmask 255.255.255.0
        gateway 192.168.1.1

$ grep -A3 '^iface eth0' file3
iface eth0 inet static
        address 192.168.1.123
        netmask 255.255.255.0
        gateway 192.168.1.1


论坛徽章:
20
卯兔
日期:2015-01-26 22:05:142015亚冠之萨济拖拉机
日期:2015-09-10 15:15:282015亚冠之阿尔希拉尔
日期:2015-09-25 17:37:53程序设计版块每日发帖之星
日期:2015-10-03 06:20:00程序设计版块每日发帖之星
日期:2015-12-09 06:20:00CU十四周年纪念徽章
日期:2015-12-17 09:07:15程序设计版块每日发帖之星
日期:2015-12-25 06:20:34程序设计版块每日发帖之星
日期:2015-12-25 06:20:34程序设计版块每日发帖之星
日期:2015-12-25 06:20:342015亚冠之广州富力
日期:2015-08-27 19:29:56每日论坛发贴之星
日期:2015-08-26 06:20:002015亚冠之阿尔希拉尔
日期:2015-05-18 17:26:27
6 [报告]
发表于 2015-05-21 21:38 |只看该作者
回复 1# 791605975
$sed -n 'h;/^iface eth0/{:a;H;n;/^$/!ba;g;s/\n.*//;r file1
};p' file2 > file3

$ cat file1
        address 192.168.1.123
        netmask 255.255.255.0
        gateway 192.168.1.1

$ grep -A3 '^iface eth0' file2
iface eth0 inet static
        address 192.168.1.22
        netmask 255.255.255.0
        gateway 192.168.1.1

$ grep -A3 '^iface eth0' file3
iface eth0 inet static
        address 192.168.1.123
        netmask 255.255.255.0
        gateway 192.168.1.1
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP