【问题标题】:How do you delete all lines that contain double quotes in sh?如何删除sh中所有包含双引号的行?
【发布时间】:2011-11-19 18:32:49
【问题描述】:

我试过
sed -ne '/\"/!p' theinput > theproduct
但这让我无处可去。它什么也没做。我可以尝试什么?

【问题讨论】:

    标签: macos sed terminal sh


    【解决方案1】:

    您不需要转义引号。写:

    sed '/"/d' theinput > theproduct

    sed -i '/"/d' theinput

    直接修改文件。

    如果您有 @Jonathan Leffler 建议的其他引用,您必须找出哪些引用。然后,使用 \x 你可以实现你想要的。 \x 用于指定十六进制值。

    sed -i '/\x22/d' theinput

    上面的行将删除输入中包含普通 (ASCII 34) 引号的所有行。您必须尝试 Jonathan 建议的代码点。

    【讨论】:

    • 在什么情况下不起作用?在我使用/bin/sh 作为当前shell 的MacOS X 终端上,它运行良好。您是否有机会处理 UTF-8 数据和魔术双引号(不是 ASCII 34 或 Unicode U+0022,而是其他东西)?
    【解决方案2】:

    试试这个:

    grep -v '"' theinput > theproduct
    

    【讨论】:

    • 也许如果您要发布您的输入示例,我们可以为您提供更好的帮助。
    • 也向我们展示输出,如果有的话。
    • @t3hcakeman:它可以满足您的要求。也许您的输入不是您认为的那样,或者您没有正确检查输出文件theproduct
    【解决方案3】:

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

    $ 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 命令。

    【讨论】:

      【解决方案4】:

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

      替代的 Unicode '双引号'可以是:

      • U+2033 双素数; U+201C 左双引号;
      • U+201D 右双引号;
      • U+201F 双高反转 9 引号;
      • U+02DD 双重音;
      • (我可能很容易遗漏了其他人)。

      您可以使用od 命令进行调试:

      $ 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 是我自己的以十六进制转储数据的命令。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-07-27
        • 1970-01-01
        • 1970-01-01
        • 2023-01-11
        • 2013-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多