zsh sed用特殊字符扩展变量并保留它们 [英] zsh sed expanding a variable with special characters and keeping them

查看:251
本文介绍了zsh sed用特殊字符扩展变量并保留它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串存储在变量中,然后在sed命令中扩展该变量.

I'm trying to store a string in a variable, then expand that variable in a sed command.

在调用命令之前,我将在变量中放入的几个值将带有括号(左括号前有斜杠,无斜杠,但右括号前无斜杠),换行符和其他特殊字符.另外,该字符串将在正在搜索的文件中用双引号引起来,我想用那些双引号将其限制为我要查询的字符串.

Several of the values I'm going to put in the variable before calling the command will have parentheses (with and without slashes before the left parentheses, but never before the right), new lines and other special characters. Also, the string will have double quotes around it in the file that's being searched, and I'd like to use those to limit only to the string I'm querying.

该命令必须能够与文件中的那些特殊字符匹配.使用zsh/Mac OS,尽管如果该命令与bash 4.2兼容,那将是一个不错的选择.回显xargs也很好.另外,如果awk对此更好,则我不需要使用sed.

The command needs to be able to match with those special characters in the file. Using zsh / Mac OS, although if the command was compatible with bash 4.2 that'd be a nice bonus. echoing to xargs is fine too. Also, if awk would be better for this, I have no requirement to use sed.

类似...

sed 's/"\"$(echo -E - ${val})\""/"${key}.localized"/g' "${files}"

鉴于 $ val 是我上面所述的变量, $ key 没有空格(但带有下划线)& $ files 是文件路径的数组(最好与空格兼容,但不是必需的).

Given that $val is the variable I described above, $key has no spaces (but underscores) & $files is an array of file paths (preferably compatible with spaces, but not required).

$ val ...的示例输入值...

Example Input values for $val...

... "something \(customStringConvertible) here" ...

... "something (notVar) here" ...

... "something %@ here" ...

... "something @ 100% here" ...

... "something for $100.00" ...

示例输出:

... "some_key".localized ...

我正在使用sed命令替换上面的示例.我覆盖它的文本非常简单.

I was using the sed command to replace the examples above. The text I'm overwriting it with is pretty straight forward.

我遇到的关键问题是获取与特殊字符匹配的命令,而不是扩展它们然后尝试匹配.

The key problem I'm having is getting the command to match with the special characters instead of expanding them and then trying to match.

在此先感谢您的帮助.

推荐答案

awk更好,因为它提供了适用于文字字符串的函数:

awk is better since it provides functions that work with literal strings:

$ val='something \(customStringConvertible) here' awk 'index($0,ENVIRON["val"])' file
... "something \(customStringConvertible) here" ...

$ val='something for $100.00' awk 'index($0,ENVIRON["val"])' file
... "something for $100.00" ...

上面是在此输入文件上运行的:

The above was run on this input file:

$ cat file
... "something \(customStringConvertible) here" ...
... "something (notVar) here" ...
... "something %@ here" ...
... "something @ 100% here" ...
... "something for $100.00" ...

使用sed,您必须遵循

With sed you'd have to follow the instructions at Is it possible to escape regex metacharacters reliably with sed to try to fake sed out.

目前尚不清楚您的真正目标是什么,因此,如果需要更多帮助,请编辑问题以提供简洁,可测试的样本输入和预期输出.话虽如此,看来您正在进行替代,所以也许这就是您想要的:

It's not clear what your real goal is so edit your question to provide concise, testable sample input and expected output if you need more help. Having said that, it looks like you're doing a substitution so maybe this is what you want:

$ old='"something for $100.00"' new='here & there' awk '
    s=index($0,ENVIRON["old"]) { print substr($0,1,s-1) ENVIRON["new"] substr($0,s+length(ENVIRON["old"])) }
' file
... here & there ...

或者,如果您愿意:

$ old='"something for $100.00"' new='here & there' awk '
    BEGIN { old=ENVIRON["old"]; new=ENVIRON["new"]; lgth=length(old) }
    s=index($0,old) { print substr($0,1,s-1) new substr($0,s+lgth) }
' file

或:

awk '
    BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; lgth=length(old) }
    s=index($0,old) { print substr($0,1,s-1) new substr($0,s+lgth) }
' '"something for $100.00"' 'here & there' file
... here & there ...

请参阅如何在awk脚本?,以获取有关我如何使用上述ENVIRON[]ARGV[]的信息.

See How do I use shell variables in an awk script? for info on how I'm using ENVIRON[] vs ARGV[] above.

这篇关于zsh sed用特殊字符扩展变量并保留它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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