如何删除 sh 中所有包含双引号的行?

发布于 2024-12-05 08:24:57 字数 100 浏览 2 评论 0原文

我尝试了
sed -ne '/\"/!p' theinput > theproduct
但是没有任何结果。它没有做任何事情。我可以尝试什么?

I tried
sed -ne '/\"/!p' theinput > theproduct
but that got me nowhere. It didn't do anything. What can I try?

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

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

发布评论

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

评论(4

二智少女猫性小仙女 2024-12-12 08:24:57

您不需要转义引号。写入:

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.

滿滿的愛 2024-12-12 08:24:57

试试这个:

grep -v '"' theinput > theproduct

try this:

grep -v '"' theinput > theproduct
笛声青案梦长安 2024-12-12 08:24:57

您向我们展示的命令应该有效。

$ cat theinput 
foo"bar
foo.bar
$ sed -ne '/\"/!p' theinput > theproduct
$ cat theproduct 
foo.bar
$ 

除非您使用 csh 或 tcsh 作为交互式 shell。在这种情况下,您需要转义 ! 字符,即使在引号内:

% cat theinput 
foo"bar
foo.bar
% sed -ne '/\"/!p' theinput > theproduct
sed -ne '/"/pwd' theinput > theproduct
sed: -e expression #1, char 5: extra characters after command
% rm theproduct 
% sed -ne '/\"/\!p' theinput > theproduct
% cat theproduct 
foo.bar
% 

但这与您的声明“它没有做任何事情”不一致,因此不清楚到底发生了什么(问题被标记为

但是有更简单的方法可以完成相同的任务,特别是@Mike Sokolov 建议的 grep 命令。

The command you showed us should have worked.

$ cat theinput 
foo"bar
foo.bar
$ sed -ne '/\"/!p' theinput > theproduct
$ cat theproduct 
foo.bar
$ 

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:

% cat theinput 
foo"bar
foo.bar
% sed -ne '/\"/!p' theinput > theproduct
sed -ne '/"/pwd' theinput > theproduct
sed: -e expression #1, char 5: extra characters after command
% rm theproduct 
% sed -ne '/\"/\!p' theinput > theproduct
% cat theproduct 
foo.bar
% 

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 anyway).

But there are much simpler ways to accomplish the same task, particularly the grep command suggested by @Mike Sokolov.

近箐 2024-12-12 08:24:57

您确定您有“ASCII”输入吗?您是否可以使用 Unicode (UTF-8),其字符不是 ASCII 34 或 Unicode U+0022,而是其他字符?

替代的 Unicode“双引号”可能是:

  • U+2033 DOUBLE PRIME; U+201C 左双引号;
  • U+201D 右双引号;
  • U+201F 双高反转 9 引号;
  • U+02DD 双锐音符;
  • (很可能还有其他我遗漏的)。

您可以使用 odx 命令来调试它:

$ cat theinput
No double quote here
Double quote " here
Unicode pseudo-double-quotes include “”‟″˝.
$ od -c theinput
0000000    N   o       d   o   u   b   l   e       q   u   o   t   e    
0000020    h   e   r   e  \n   D   o   u   b   l   e       q   u   o   t
0000040    e       "       h   e   r   e  \n   U   n   i   c   o   d   e
0000060        p   s   e   u   d   o   -   d   o   u   b   l   e   -   q
0000100    u   o   t   e   s       i   n   c   l   u   d   e       “  **
0000120   **   ”  **  **   ‟  **  **   ″  **  **   ˝  **   .  \n        
0000136
$ od -x theinput
0000000      6f4e    6420    756f    6c62    2065    7571    746f    2065
0000020      6568    6572    440a    756f    6c62    2065    7571    746f
0000040      2065    2022    6568    6572    550a    696e    6f63    6564
0000060      7020    6573    6475    2d6f    6f64    6275    656c    712d
0000100      6f75    6574    2073    6e69    6c63    6475    2065    80e2
0000120      e29c    9d80    80e2    e29f    b380    9dcb    0a2e        
0000136
$ odx theinput
0x0000: 4E 6F 20 64 6F 75 62 6C 65 20 71 75 6F 74 65 20   No double quote 
0x0010: 68 65 72 65 0A 44 6F 75 62 6C 65 20 71 75 6F 74   here.Double quot
0x0020: 65 20 22 20 68 65 72 65 0A 55 6E 69 63 6F 64 65   e " here.Unicode
0x0030: 20 70 73 65 75 64 6F 2D 64 6F 75 62 6C 65 2D 71    pseudo-double-q
0x0040: 75 6F 74 65 73 20 69 6E 63 6C 75 64 65 20 E2 80   uotes include ..
0x0050: 9C E2 80 9D E2 80 9F E2 80 B3 CB 9D 2E 0A         ..............
0x005E:
$ sed '/"/d' theinput > theproduct
$ cat theproduct
No double quote here
Unicode pseudo-double-quotes include “”‟″˝.
$ 

(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:

  • U+2033 DOUBLE PRIME; U+201C LEFT DOUBLE QUOTATION MARK;
  • U+201D RIGHT DOUBLE QUOTATION MARK;
  • U+201F DOUBLE HIGH-REVERSED-9 QUOTATION MARK;
  • U+02DD DOUBLE ACUTE ACCENT;
  • (and there could easily be others I've left out).

You can look to debug this with the od command:

$ cat theinput
No double quote here
Double quote " here
Unicode pseudo-double-quotes include “”‟″˝.
$ od -c theinput
0000000    N   o       d   o   u   b   l   e       q   u   o   t   e    
0000020    h   e   r   e  \n   D   o   u   b   l   e       q   u   o   t
0000040    e       "       h   e   r   e  \n   U   n   i   c   o   d   e
0000060        p   s   e   u   d   o   -   d   o   u   b   l   e   -   q
0000100    u   o   t   e   s       i   n   c   l   u   d   e       “  **
0000120   **   ”  **  **   ‟  **  **   ″  **  **   ˝  **   .  \n        
0000136
$ od -x theinput
0000000      6f4e    6420    756f    6c62    2065    7571    746f    2065
0000020      6568    6572    440a    756f    6c62    2065    7571    746f
0000040      2065    2022    6568    6572    550a    696e    6f63    6564
0000060      7020    6573    6475    2d6f    6f64    6275    656c    712d
0000100      6f75    6574    2073    6e69    6c63    6475    2065    80e2
0000120      e29c    9d80    80e2    e29f    b380    9dcb    0a2e        
0000136
$ odx theinput
0x0000: 4E 6F 20 64 6F 75 62 6C 65 20 71 75 6F 74 65 20   No double quote 
0x0010: 68 65 72 65 0A 44 6F 75 62 6C 65 20 71 75 6F 74   here.Double quot
0x0020: 65 20 22 20 68 65 72 65 0A 55 6E 69 63 6F 64 65   e " here.Unicode
0x0030: 20 70 73 65 75 64 6F 2D 64 6F 75 62 6C 65 2D 71    pseudo-double-q
0x0040: 75 6F 74 65 73 20 69 6E 63 6C 75 64 65 20 E2 80   uotes include ..
0x0050: 9C E2 80 9D E2 80 9F E2 80 B3 CB 9D 2E 0A         ..............
0x005E:
$ sed '/"/d' theinput > theproduct
$ cat theproduct
No double quote here
Unicode pseudo-double-quotes include “”‟″˝.
$ 

(odx is my own command for dumping data in hex.)

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