【问题标题】:Replace a string if a particular substring exists?如果存在特定的子字符串,则替换字符串?
【发布时间】:2017-02-28 10:39:24
【问题描述】:

当且仅当该行中存在一个字符串时,我想替换一个字符串。应该对文件夹中的所有文件执行此操作。

1. This is My String.
2. Get This string as input.

在上述两个字符串中,如果存在 'String',请将 My 替换为 your >.

输出应该是这样的

1. This is your String.
2. Get This string as input.

【问题讨论】:

    标签: string macos shell


    【解决方案1】:
    sed '/String/ s/My/YOUR/g' input
    

    awk '/String/{gsub(/My/,"YOUR")}1' input
    

    【讨论】:

    • 输出甚至应该反映在文件中。我想更改文件夹中的所有文件。
    • 使用sed -i 选项或检查awk 是否有及时更换。
    猜你喜欢
    • 2018-11-09
    • 2013-02-18
    • 2022-11-12
    • 2012-09-04
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2012-04-28
    相关资源
    最近更新 更多