sed的正则表达式中的转义美元符号 [英] Escape dollar sign in regexp for sed

查看:240
本文介绍了sed的正则表达式中的转义美元符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实际询问之前,我将介绍我的问题-随时跳过此部分!

有关我的设置的一些背景信息

要在软件系统中手动更新文件,我正在创建一个bash脚本,以使用diff删除新版本中不存在的所有文件:

To update files manually in a software system, I am creating a bash script to remove all files that are not present in the new version, using diff:

for i in $(diff -r old new 2>/dev/null | grep "Only in old" | cut -d "/" -f 3- | sed "s/: /\//g"); do echo "rm -f $i" >> REMOVEOLDFILES.sh; done

这很好.但是,显然我的文件的文件名中经常带有美元符号($),这是由于GWT框架的某些排列所致.这是上面创建的bash脚本的示例行:

This works fine. However, apparently my files often have a dollar sign ($) in the filename, this is due to some permutations of the GWT framework. Here is one example line from the above created bash script:

rm -f var/lib/tomcat7/webapps/ROOT/WEB-INF/classes/ExampleFile$3$1$1$1$2$1$1.class

执行此脚本不会删除所需的文件,因为bash会将这些文件作为参数变量读取.因此,我必须用"\ $"转义美元符号.

Executing this script would not remove the wanted files, because bash reads these as argument variables. Hence I have to escape the dollar signs with "\$".

我的实际问题

我现在想在上述管道中添加一个sed-Command,以替换该美元符号.实际上,sed还将美元符号读作正则表达式的特殊字符,因此显然我也必须转义它. 但是以某种方式这是行不通的,经过大量搜索后我找不到解释.

I now want to add a sed-Command in the aforementioned pipeline, replacing this dollar sign. As a matter of fact, sed also reads the dollar sign as special character for regular expressions, so obviously I have to escape it as well. But somehow this doesn't work and I could not find an explanation after googling a lot.

这是我尝试过的一些变化:

Here are some variations I have tried:

echo "Bla$bla" | sed "s/\$/2/g"        # Output: Bla2
echo "Bla$bla" | sed 's/$$/2/g'        # Output: Bla
echo "Bla$bla" | sed 's/\\$/2/g'       # Output: Bla
echo "Bla$bla" | sed 's/@"\$"/2/g'     # Output: Bla
echo "Bla$bla" | sed 's/\\\$/2/g'      # Output: Bla

在此示例中,所需的输出应为"Bla2bla". 我想念什么? 我正在使用GNU sed 4.2.2

The desired output in this example should be "Bla2bla". What am I missing? I am using GNU sed 4.2.2

编辑

我刚刚意识到,上面的示例开头是错误的-echo命令已经将$解释为变量,而下面的sed仍然无法得到它……这里是一个正确的示例:

I just realized, that the above example is wrong to begin with - the echo command already interprets the $ as a variable and the following sed doesn't get it anyway... Here a proper example:

  1. 创建内容为bla$bla
  2. 的文本文件test
  3. cat test给出bla$bla
  4. cat test | sed "s/$/2/g"给出bla$bla2
  5. cat test | sed "s/\$/2/g"给出bla$bla2
  6. cat test | sed "s/\\$/2/g"给出bla2bla
  1. Create a textfile test with the content bla$bla
  2. cat test gives bla$bla
  3. cat test | sed "s/$/2/g" gives bla$bla2
  4. cat test | sed "s/\$/2/g" gives bla$bla2
  5. cat test | sed "s/\\$/2/g" gives bla2bla

因此,最后一个版本就是答案.请记住:在测试时,首先要确保测试正确,然后再对测试对象提出疑问........

Hence, the last version is the answer. Remember: when testing, first make sure your test is correct, before you question the test object........

推荐答案

脚本还有其他问题,但是如果在生成的脚本中正确引用rm的参数,则包含$的文件名也不是问题.

There are other problems with your script, but file names containing $ are not a problem if you properly quote the argument to rm in the resulting script.

echo "rm -f '$i'" >> REMOVEOLDFILES.sh

或使用printf,这样可以使报价更好一些,并且更易于移植:

or using printf, which makes quoting a little nicer and is more portable:

printf "rm -f '%s'" "$i" >> REMOVEOLDFILES.sh

(请注意,我要解决的是真正的问题,不一定是您提出的问题.)

(Note that I'm addressing the real problem, not necessarily the question you asked.)

这篇关于sed的正则表达式中的转义美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆