Macos 如何删除sh中包含双引号的所有行?
我试过产品的sed-ne'/\“/!p'theinput>,但毫无结果。它什么也没做。我能试什么吗?试试这个:Macos 如何删除sh中包含双引号的所有行?,macos,sed,terminal,sh,bourne-shell,Macos,Sed,Terminal,Sh,Bourne Shell,我试过产品的sed-ne'/\“/!p'theinput>,但毫无结果。它什么也没做。我能试什么吗?试试这个: grep -v '"' theinput > theproduct 您不需要转义引号。请编写: sed'/“/d”输入>产品 或 sed-i'/“/d”输入 直接更改文件 如果您有@Jonathan Leffler建议的其他引号,您必须找出哪些引号。然后,使用\x可以实现您想要的。\x用于指定十六进制值 sed-i'/\x22/d'输入 上面的一行将删除输入中包含普通(ASCI
grep -v '"' theinput > theproduct
您不需要转义引号。请编写:
sed'/“/d”输入>产品
或
sed-i'/“/d”输入
直接更改文件
如果您有@Jonathan Leffler建议的其他引号,您必须找出哪些引号。然后,使用\x可以实现您想要的。\x用于指定十六进制值
sed-i'/\x22/d'输入
上面的一行将删除输入中包含普通(ASCII 34)引号的所有行。您必须尝试Jonathan建议的代码点。您向我们展示的命令应该可以工作
$ 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
命令。你确定你有“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
是我自己用十六进制转储数据的命令。)也许如果您发布一个输入样本,我们可以更好地帮助您。如果有,也向我们展示输出。@t3hcakeman:它符合您的要求。也许您的输入不是您所认为的,或者您没有正确检查输出文件产品
?它以什么方式不起作用?在我的MacOS X终端上使用/bin/sh
作为当前的shell,它工作得很好。您是否有可能处理UTF-8数据和神奇的双引号(不是ASCII 34或Unicode U+0022,而是其他东西)?