我想知道是否有办法在 bash 中使用 multilple regexp 替换 ${string//substring/replacement} 或者,可能存在更好的解决方案。
我有一个脚本可以使用 curl 将更新发送到 statusnet 和 friendika。 例如,我在网上签署请愿书,并被邀请在推特上发布,但我宁愿发送到 identica。我厌倦了粘贴东西并且不得不在终端中编辑以逃避@和#,!和 ?。 我想在我的脚本中用正则表达式替换它们以自动更改
@repbunghole and @SenatorArsehat Stop farking around with #someship! Do you want me to come smack you? | http://someshort.url
到
\@repbunghole \@SenatorArsehat Stop farking around with \#someship\! Do you want me to come smack you\? \| http://someshort.url
我没有强大的 sed 或 awk fu,但想象他们可能会提供解决方案,而且我不知道如何在不将变量写入文件、读取文件并对其执行操作,然后设置 var 的情况下使用 sed使用 var=$(cat 文件)。是的。我在这方面很陌生。 我没有用上面的 ${string//substring/replacement/} 找到足够的数据来进行多次替换。运行 X 次以转义 X 个不同的字符似乎效率很低。
喜欢
read -p "Enter a string: " a
b=${a//\@/\\\@}
c=${b//\#/\\\#}
d=${c//\!/\\\!}
e=${d//\?/\\\?}
f=${e//\"/\\\"}
g=${f//\'/\\\'}
等等等等 同时工作,但它很丑......
最佳答案
这就是character classes用于:
b=${a//[@#!?"']/\\\0}
关于regex - bash中变量的多个正则表达式替换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7585564/