Mac 上的 Sed 正则表达式问题,在 Linux 上运行良好
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6761361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sed regex problem on Mac, works fine on Linux
提问by JohnSmith
This works fine on Linux (Debian):
这在 Linux (Debian) 上运行良好:
sed -e 's,^[ \t]*psd\(.*\)\;,,'
On mac, I believe I have to use the -E
flag, instead of -e
:
在 mac 上,我相信我必须使用-E
标志,而不是-e
:
sed -E 's,^[ \t]*psd\(.*\)\;,,'
but the regexp does not match, and hence does not remove the lines I want.
但正则表达式不匹配,因此不会删除我想要的行。
Any tips on how to solve this?
有关如何解决此问题的任何提示?
Sample input:
样本输入:
apa
bepa
psd(cepa);
depa psd(epa);
psd(fepa gepa hepa);
For that input, the expected output is:
对于该输入,预期输出为:
apa
bepa
depa psd(epa);
采纳答案by Michael J. Barber
The -E
flag means to use extended regular expressions. You should just use -e
, as on Linux. The sed
in Mac OS X is based on BSD sed, so doesn't have the GNU extensions.
该-E
标志表示使用扩展正则表达式。您应该只使用-e
, 就像在 Linux 上一样。将sed
在Mac OS X是基于BSD sed的,所以不具备GNU扩展。
After copying your sample input:
复制您的示例输入后:
[~ 507] pbpaste | sed -e 's,^[[:space:]]*psd\(.*\);,,'
apa
bepa
depa psd(epa);
回答by user478681
I've check this sample input on my machine and faced the problem when in third line was tab character from the beginning of line and regexp ^[ \t]*psd\(.*\)\;
didn't match it. This can be passed by sed character class [[:blank:]]
that equal combination of space and tab character. So you can try the following:
我已经在我的机器上检查了这个示例输入,并且在第三行是从行首开始的制表符并且正则表达式^[ \t]*psd\(.*\)\;
不匹配时遇到了问题。这可以由[[:blank:]]
等于空格和制表符组合的sed 字符类传递。因此,您可以尝试以下操作:
sed -E 's,^[[:blank:]]*psd\(.*\)\;,,' demo.txt
this produce the following output:
这会产生以下输出:
apa
bepa
depa psd(epa);
but it keeps the empty lines in result. To get the exact output as you expected I used the following:
但它在结果中保留了空行。为了获得您预期的确切输出,我使用了以下内容:
sed -n '/^[[:blank:]]*psd\(.*\)\;/!p' demo.txt
result:
结果:
apa
bepa
depa psd(epa);
this is just inverse output of matching pattern (!p
).
这只是匹配模式 ( !p
) 的逆输出。
EDIT:To match tab characters in regexp in sed (macosx) you can also try recommendation from How can I insert a tab character with sed on OS X?
编辑:要在 sed (macosx) 中匹配正则表达式中的制表符,您还可以尝试从如何在 OS X 上插入带有 sed 的制表符的建议?
回答by jfg956
The '\t'
is not standard in 'sed'
, it is a GNU extension.
在'\t'
没有标准'sed'
,它是GNU扩展。
To match a 'tab'
, you need to put a real 'tab'
in your script. This is easy in a file, harder in shell.
要匹配 a 'tab'
,您需要'tab'
在脚本中放入一个 real 。这在文件中很容易,在 shell 中更难。
The same problem can happen in AIX, Solaris and HP-UX or other UNIXes.
同样的问题可能发生在 AIX、Solaris 和 HP-UX 或其他 UNIX 中。
回答by a.b.d
Alternatively you can use the GNU version of sed instead of the implementation provided by Mac OSX.
或者,您可以使用 sed 的 GNU 版本而不是 Mac OSX 提供的实现。
Mac portprovides a port for it sudo port install gsed
. After installing it you can use gsed
instead of sed
.
Mac 端口为其提供了一个端口sudo port install gsed
。安装后,您可以使用gsed
代替sed
.
回答by waku
In addition to the answers above, you can exploit a useful (but shell-dependent) trick. In bash, use $'\t'
to introduce a literal tab character. The following works on my Mac:
除了上面的答案之外,您还可以利用一个有用的(但依赖于 shell)的技巧。在 bash 中,用于$'\t'
引入文字制表符。以下适用于我的 Mac:
sed -e 's,^[ '$'\t''*psd\(.*\);,,'
sed -e 's,^[ '$'\t''*psd\(.*\);,,'
Note how the whole sed expression consists now of three concatenated strings.
请注意整个 sed 表达式现在是如何由三个连接的字符串组成的。
This trick might be useful in case you need the tab character specifically, without matching other whitespace (i.e., when [[:blank:]]
would be too inclusive). For the above, the -e flag is not essential.
如果您特别需要制表符,而不匹配其他空格(即,何时[[:blank:]]
包含太多空格),则此技巧可能很有用。对于上述情况,-e 标志不是必需的。