替换命令有问题

发布于 2024-09-27 10:34:49 字数 468 浏览 6 评论 0原文

我在替换命令时遇到一些问题。

输入文件我有这个

{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}

我想用单引号替换

'{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}'

使用下面提到的命令

find input.txt -exec sed 's/{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}/'{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}'/g' {} \;

我收到这样的错误

bash: syntax error near unexpected token `('

I have some problem with replace command.

Input file i have this

{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}

I want to replace with single quotes

'{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}'

Using the below mentioned command

find input.txt -exec sed 's/{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}/'{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}'/g' {} \;

I am getting error like this

bash: syntax error near unexpected token `('

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

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

发布评论

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

评论(4

旧人九事 2024-10-04 10:34:49

首先,您无法在用单引号分隔的参数中轻松插入文字单引号。
您必须将 ' 替换为 '"'"''\''
作为猜测这个问题的提示,这里,投诉来自 shell,这意味着甚至你的命令行也是格式错误的。

其次,您应该知道美元和括号是 sed 正则表达式中的特殊字符。你可能必须逃离他们。阅读 man sed 了解更多详细信息。

第三,我不确定 find input.txt 是否有效。我猜你的意思是 find 。 -type f -name input.txt?

First, you cannot insert easily a literal single quote in an argument that is delimited with single quotes.
You have to replace your ''s with '"'"' or '\''.
As a hint to guess this problem, here, the complaint comes from the shell, which means that even your command-line is malformed.

Second, you should be aware that dollar and parentheses are special characters in sed regular expressions. You will probably have to escape them. Read man sed for more details on this.

Third, I am not sure whether find input.txt will work. I guess you meant find . -type f -name input.txt?

你げ笑在眉眼 2024-10-04 10:34:49

这是因为替换字符串中的裸露 ' 实际上终止了 sed 命令,这意味着 shell 正在尝试处理该行。当 Benoit 的编辑使您的问题语法颜色正确时,这一点实际上立即变得显而易见。您可以看到该行的第二个 ' (替换文本的第一个字符)由于字符串终止而将颜色从红色更改为黑色。

此外,sed 不喜欢使用裸露的[],因为它们在正则表达式中指示字符类。

您可以使用以下方法解决这两个问题:

pax> find input.txt -exec sed 's/{a\[$1\]=a\[$1\]FS$2}END{for(i in a) print i,a\[i\]}/\x27{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}\x27/g' {} \;

which 输出:

'{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}'

所以基本上,转义搜索字符串中的方括号,以便将它们视为文字,并在替换字符串中使用 \x27 而不是 '< /代码>。

It's because the naked ' in your replacement string is actually terminating the sed command, meaning the shell is trying to process the line. This actually became immediately obvious when Benoit's edit caused your question to syntax-colour correctly. You can see the second ' on the line (the first character of the substitution text) changed the colour from red to black due to the string termination.

In addition, sed won't like the use of naked [] since they indicate character classes in regular expressions.

You can fix both problems with:

pax> find input.txt -exec sed 's/{a\[$1\]=a\[$1\]FS$2}END{for(i in a) print i,a\[i\]}/\x27{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}\x27/g' {} \;

which outputs:

'{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}'

So basically, escape the square brackets in the search string so they're treated as literals and use \x27 in the replacement string instead of '.

み零 2024-10-04 10:34:49

我觉得很有趣为什么你必须这样做。你应该首先编写正确的代码,因为我可以看到它的 awk 代码。难道你在创建 input.txt 文件时故意省略了单引号吗?而且它只是您正在编辑的一个文件。没有必要使用查找。 (除非你不知道它在哪里)。

I find it funny why you should have to do this. You should write the correct code in the first place, since I can see its awk code. Don't tell me you purposely omit the single quote in your input.txt file when you created it? And its only a single file you are editing. There's no need to use find. (unless you don't know where it is).

作死小能手 2024-10-04 10:34:49

最后我找到了简单的方法来做到这一点。

cat input.txt sed "s/{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}/'{a[$1]=a[$1]FS $2}END{for(i in a) print i,a[i]}'/g"

我希望我会有所帮助

Finally i found simple way to do this.

cat input.txt sed "s/{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}/'{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}'/g"

I hope i will helpful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文