带有查找条件的文件内容替换

回复 收藏
本帖最后由 butterfly梧桐雨 于 2016-4-27 15:56 编辑

今天工作上有个需求,首先有个文件,文件内容截取了一部分如下,整个文件是很多类似的重复内容:

===================================================================
        {   script => 'dsw_ot_pm_bin_Basic_Check_15MIN_1DAY_Testcase.py',
        configFile => 'dsw_ot_pm_bin_Basic_Check_Config.cfg',
        option => '--testbench 4DPA4_E2E_7 --cportrate 4DPA4_1GBE --tc_type 9 --tmper 15MIN --bin_number 6 --test_complete_and_long no --nocleanup',
    },

        {   script => 'dsw_ot_pm_bin_Basic_Check_15MIN_1DAY_Testcase.py',
        configFile => 'dsw_ot_pm_bin_Basic_Check_Config.cfg',
        option => '--testbench 4DPA4_E2E_7 --cportrate 4DPA4_1GBE --tc_type 9 --tmper 1DAY --bin_number 5 --test_complete_and_long no --nosetup --nocleanup',
    },

        {   script => 'dsw_ot_pm_bin_Basic_Check_15MIN_1DAY_Testcase.py',
        configFile => 'dsw_ot_pm_bin_Basic_Check_Config.cfg',
        option => '--testbench 4DPA4_E2E_7 --cportrate 4DPA4_1GBE --tc_type 9 --tmper 15MIN --bin_number 33 --test_complete_and_long no --nosetup --nocleanup',
    },

        {   script => 'dsw_ot_pm_bin_Basic_Check_15MIN_1DAY_Testcase.py',
        configFile => 'dsw_ot_pm_bin_Basic_Check_Config.cfg',
        option => '--testbench 4DPA4_E2E_7 --cportrate 4DPA4_1GBE --tc_type 9 --tmper 1DAY --bin_number 8 --test_complete_and_long no --nosetup',
    },
==================================================================   

需要把含有“--tmper 15MIN”的行的“--test_complete_and_long no” 替换为“--test_complete_and_long yes”,含有“--tmper 1DAY”的行不替换


而且这样的文件很多,平时也许就傻乎乎的手工一个个去改了,今天研究了一下,总算用shell命令修改成功。


结果总结如下:
1. awk不可以直接替换文件,除非考虑生成个中间文件,再覆盖
2. 用sed替换成功:
sed -i -r 's/(15MIN)(.*)(--test_complete_and_long no)/\1\2--test_complete_and_long yes/' Setup.conf

将阿铭老师的调换顺序的例子做了下修改,真好用,开心地解决了问题。
2016-04-27 15:54 举报
已邀请:

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: