bash 中变量的多个正则表达式替换?

发布于 2024-12-07 07:25:28 字数 947 浏览 0 评论 0原文

我想知道是否有办法在 bash 中进行多个正则表达式替换 ${string//substring/replacement} 或者,可能存在什么更好的解决方案。

我有一个脚本可以使用curl 向statusnet 和friendika 发送更新。 例如,我在网上签署请愿书,并被提议在推特上发布它们,但我宁愿发送到 identica。我厌倦了粘贴内容并必须在终端中编辑以转义@和#,!和 ?。 我想在我的脚本中用 regexp 替换它们以自动更改

@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,但想象它们可能提供解决方案,并且我不知道如何使用 sed 而不将变量写入文件,读取文件并对其进行操作,然后使用 var=$(cat file) 设置 var。是的。我对这件事很陌生。 我在上面的 ${string//substring/replacement/} 中找不到足够的数据来进行多次替换。运行 X 次来转义 X 个不同的字符似乎效率很低。

read -p "Enter a string: " a
b=${a//\@/\\\@}
c=${b//\#/\\\#}
d=${c//\!/\\\!}
e=${d//\?/\\\?}
f=${e//\"/\\\"}
g=${f//\'/\\\'}

等等等等 同时工作,但它很丑......

I'd like to know if there's a way to make multilple regexp replacements in bash with
${string//substring/replacement} or, possibly, what better solution exists.

I have a script to send updates to statusnet and friendika with curl.
I sign petitions online, for instance, and am offered to tweet them, but would rather send to identica. I'm tired of pasting stuff and having to edit in terminal to escape @ and #, ! and ?.
I'd like to regexp replace them in my script to automagically change

@repbunghole and @SenatorArsehat Stop farking around with #someship! Do you want me to come smack you? | http://someshort.url

to

\@repbunghole \@SenatorArsehat Stop farking around with \#someship\! Do you want me to come smack you\? \| http://someshort.url

I do not have strong sed or awk fu, but imagine they may offer solutions, and I don't know how to use sed without writing the variable to a file, reading the file and acting on it, then setting the var with var=$(cat file). Yes. I'm pretty new at this stuff.
I'm not finding sufficient data with the above ${string//substring/replacement/} for multiple replacements. Running that X times to escape X different characters seems inefficient.

like

read -p "Enter a string: " a
b=${a//\@/\\\@}
c=${b//\#/\\\#}
d=${c//\!/\\\!}
e=${d//\?/\\\?}
f=${e//\"/\\\"}
g=${f//\'/\\\'}

etc., etc.
works in the meantime, but it's ugly...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走过海棠暮 2024-12-14 07:25:28

这就是 字符类 的用途:

b=${a//[@#!?"']/\\\0}

That's what character classes are for:

b=${a//[@#!?"']/\\\0}
无尽的现实 2024-12-14 07:25:28

对于“bash 中变量的多个正则表达式替换?”

sed 和 awk 都可以做到。
替换

a->1
b->2
c->3

例如我想用 sed:

kent$  v=abc
kent$  newV=$(sed -e's/a/1/; s/b/2/; s/c/3/' <<< $v)
kent$  echo $newV2
123

awk:

kent$  v=abc
kent$  newV2=$(awk '{gsub(/a/,"1");gsub(/b/,"2");gsub(/c/,"3")}1' <<< $v)                                                                
kent$  echo $newV                                                        
123

for "multiple regex replacement on variable in bash?"

both sed and awk can do it.
e.g I want to replace

a->1
b->2
c->3

with sed:

kent$  v=abc
kent$  newV=$(sed -e's/a/1/; s/b/2/; s/c/3/' <<< $v)
kent$  echo $newV2
123

with awk:

kent$  v=abc
kent$  newV2=$(awk '{gsub(/a/,"1");gsub(/b/,"2");gsub(/c/,"3")}1' <<< $v)                                                                
kent$  echo $newV                                                        
123
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文