加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl搜索和替换

发布时间:2020-12-15 21:33:07 所属栏目:大数据 来源:网络整理
导读:如何使用搜索和替换来更改文件的内容. 我需要能够将’all to none’和/或’none to all’更改为无需每次手动执行数百行. 下面是一个示例文本文件,右侧是行号.我知道如何打开和关闭文件进行写入. 行 1 define_filter absolute_minimal_filter ——- all 2 def
如何使用搜索和替换来更改文件的内容.

我需要能够将’all to none’和/或’none to all’更改为无需每次手动执行数百行.

下面是一个示例文本文件,右侧是行号.我知道如何打开和关闭文件进行写入.

1 define_filter absolute_minimal_filter ——- all
2 define_filter atma_basic_filter ——– —–无
3 define_filter atma_communication_filter – 全部
4 define_filter atma_health_filter ————-无
5 define_filter atma_misc_filter ————— all
6 define_filter atma_performance_filter —- none
7 define_filter atma_supplemental_filter —- none

解决方法

使用精心挑选的字符串代替“temp”(在两个地方):
sed 's/all/temp/g;s/none/all/g; s/temp/none/g' input_file > output_file

或者没有临时字符串,如果“all”和“none”永远不会出现在同一行并始终结束该行:

sed 's/all$/none/;t;s/none$/all/' input_file > output_file

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读