为什么grep不从安全实用程序的输出中提取?可比较地从回声作品中提取

时间:2022-11-28 14:07:11

In OS X, there is a command-line utility called security which is designed to access the Keychain. It can return the password of an entry in the Keychain, which is what I'm trying to do. When I run security find-internet-password -ga an_entry | grep "password" I get the following output, as expected:

在OS X中,有一个名为security的命令行实用程序,用于访问密钥链。它可以返回键链中条目的密码,这就是我要做的。当我运行安全查找-internet-password -ga an_entry | grep“password”时,我得到如下输出,如预期:

password: "apassword123"

I would like to extract the text between the quotes. grep, specifically pcregrep, should work but doesn't:

我想在引号之间提取文本。grep,尤其是pcregrep,应该有效,但没有:

echo 'password: "apassword123"' | pcregrep -o '^password:\s"(?!_)\K[^"]+'

Returns:

返回:

apassword123

As expected, but:

正如所料,但是:

security find-internet-password -ga anentry | pcregrep -o '^password:\s"(?!_)\K[^"]+'

Returns:

返回:

password: "apassword123"

Which is absurd.

这是荒谬的。

1 个解决方案

#1


2  

Your output actually doesn't get through the grep as it's on stderr. You should be able to do this by redirecting the stderr to stdout like

您的输出实际上没有通过grep,因为它在stderr上。您应该能够通过像这样将stderr重定向到stdout来实现这一点

security find-internet-password -ga anentry 2>&1 | pcregrep -o '^password:\s"(?!_)\K[^"]+'

#1


2  

Your output actually doesn't get through the grep as it's on stderr. You should be able to do this by redirecting the stderr to stdout like

您的输出实际上没有通过grep,因为它在stderr上。您应该能够通过像这样将stderr重定向到stdout来实现这一点

security find-internet-password -ga anentry 2>&1 | pcregrep -o '^password:\s"(?!_)\K[^"]+'