如何删除 sh 中所有包含双引号的行?
我尝试了sed -ne '/\"/!p' theinput > theproduct
但是没有任何结果。它没有做任何事情。我可以尝试什么?
I triedsed -ne '/\"/!p' theinput > theproduct
but that got me nowhere. It didn't do anything. What can I try?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要转义引号。写入:
sed '/"/d' theinput > theproduct
或
sed -i '/"/d' theinput
直接更改文件。
如果您有@Jonathan Leffler 建议的其他引言,您必须找出哪些引言。然后,使用 \x 你可以实现你想要的。 \x 用于指定十六进制值。
sed -i '/\x22/d' theinput
上面的行将删除 input 中包含普通 (ASCII 34) 引号的所有行。您必须尝试乔纳森建议的代码点。
You don't need to escape quote. Write:
sed '/"/d' theinput > theproduct
or
sed -i '/"/d' theinput
to alter the file directly.
In case you have other quotes as @Jonathan Leffler suggests, you have to find out which ones. Then, using \x you can achieve what you want. \x is used to specify hexadecimal values.
sed -i '/\x22/d' theinput
The line above would delete all rows in theinput containing the ordinary (ASCII 34) quote. You'll have to try the code points Jonathan suggested.
试试这个:
try this:
您向我们展示的命令应该有效。
除非您使用 csh 或 tcsh 作为交互式 shell。在这种情况下,您需要转义
!
字符,即使在引号内:但这与您的声明“它没有做任何事情”不一致,因此不清楚到底发生了什么(问题被标记为 bourne-shell
但是有更简单的方法可以完成相同的任务,特别是@Mike Sokolov 建议的 grep 命令。
The command you showed us should have worked.
unless you're using csh or tcsh as your interactive shell. In that case, you'd need to escape the
!
character, even within quotation marks:But that's inconsistent with your statement that "It didn't do anything", so it's not clear what's really going on (and the question is tagged bourne-shell anyway).
But there are much simpler ways to accomplish the same task, particularly the
grep
command suggested by @Mike Sokolov.您确定您有“ASCII”输入吗?您是否可以使用 Unicode (UTF-8),其字符不是 ASCII 34 或 Unicode U+0022,而是其他字符?
替代的 Unicode“双引号”可能是:
您可以使用 odx 命令来调试它:
(odx 是我自己的用于以十六进制转储数据的命令。)
Are you sure you have 'ASCII' input? Could you have Unicode (UTF-8) with characters that are not not ASCII 34, or Unicode U+0022, but something else?
Alternative Unicode 'double quotes' could be:
You can look to debug this with the
od
command:(
odx
is my own command for dumping data in hex.)