【问题标题】:RegEx to replace password in Linux shell script?正则表达式替换 Linux shell 脚本中的密码?
【发布时间】:2019-07-17 14:17:32
【问题描述】:

我有一个字符串,其中包含密码等参数。-P 之后的任何内容都是密码,后跟 -H 主机等其他参数。参数可以按任意顺序排列

Ex1:sh script.ksh -U test_user -P somepwd -H somehost

Ex2:sh script.ksh -U test_user -H somehost -P somepwd

现在,我想用删除这个词替换密码

结果1:sh script.ksh -U test_user -P REMOVED -H somehost

结果2:sh script.ksh -U test_user -H somehost -P REMOVED

如何,是可能的。我尝试了以下方法。

sed -i -E "s/(.*[-]P)(\b.*\b)(.*)/\1\something\3/" test_pwd_replace.txt

但是,它不起作用。请帮忙。

【问题讨论】:

    标签: regex linux shell


    【解决方案1】:

    你可以使用这个sed:

    sed -i -E 's/-P[[:blank:]]*[^[:blank:]]+/-P REMOVED/' test_pwd_replace.txt
    

    [[:blank:]] 等价于匹配空格或制表符的[ \t]

    【讨论】:

      猜你喜欢
      • 2015-07-15
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-12
      • 2012-01-25
      • 2019-03-27
      相关资源
      最近更新 更多