替换命令有问题
我在替换命令时遇到一些问题。
输入文件我有这个
{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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您无法在用单引号分隔的参数中轻松插入文字单引号。
您必须将
'
替换为'"'"'
或'\''
。作为猜测这个问题的提示,这里,投诉来自 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 meantfind . -type f -name input.txt
?这是因为替换字符串中的裸露
'
实际上终止了sed
命令,这意味着 shell 正在尝试处理该行。当 Benoit 的编辑使您的问题语法颜色正确时,这一点实际上立即变得显而易见。您可以看到该行的第二个'
(替换文本的第一个字符)由于字符串终止而将颜色从红色更改为黑色。此外,
sed
不喜欢使用裸露的[]
,因为它们在正则表达式中指示字符类。您可以使用以下方法解决这两个问题:
which 输出:
所以基本上,转义搜索字符串中的方括号,以便将它们视为文字,并在替换字符串中使用
\x27
而不是'< /代码>。
It's because the naked
'
in your replacement string is actually terminating thesed
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:
which outputs:
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'
.我觉得很有趣为什么你必须这样做。你应该首先编写正确的代码,因为我可以看到它的 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).最后我找到了简单的方法来做到这一点。
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