开发者

Bash Scripting - Using Entry immediately after certain phrase

In my project, addgpg-apt (https://launchpad.net/addgpg-apt), I'd like to be able to have input sent into the program. From that input, say...

Unable to verify signatures in PPA. Check that this is fixed: NO_PUBKEY <PGPkeyID>

... how can I, using basic Bash, grep, etc. get that <PGPkeyID> from the string and ignore everything else in the string? (Note that these errors are generated by apt-get/apt, and as such the end of the string is always NO_PUBKEY <PGPkeyID>)

In Java, this could be done with substring, and grab only that PGPkeyID based on the location of the phrase NO_PUBKEY, but I开发者_如何学C want this to be done in Bash only, so any solutions would be appreciated.


Piping to grep -o "NO_PUBKEY <.*>" | sed -e 's/.*<\(.*\)>.*/\1/'

will yield you:

PGPkeyID


Update

Assuming your input is like this:

The following signatures couldn't be verified because the public key is not available: NO_PUBKEY CAFE0123DEADBEEF
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 0123DEADBEEFCAFE
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY DEADBEEFCAFE0123
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY BEEFCAFE0123DEAD
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY CAFE0123DEADBEEF

The following command will extract the keys:

grep -o 'PUBKEY [A-F0-9]\{16\}' | cut -f2 -d" " | sort -u

Like this:

0123DEADBEEFCAFE
BEEFCAFE0123DEAD
CAFE0123DEADBEEF
DEADBEEFCAFE0123


You can do that in pure shell without spawning a process:

cat << EOF > file
...
Unable to verify signatures in PPA. Check that this is fixed: NO_PUBKEY <PGPkeyID1>
...
Unable to verify signatures in PPA. Check that this is fixed: NO_PUBKEY <PGPkeyID2>
...
EOF

cat file | while read line; do
  if [[ $line == *\ NO_PUBKEY\ * ]]
  then
    echo ${line#* NO_PUBKEY }
  fi
done
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜