使用sed删除多个AWS错误

时间:2018-05-11 18:02:21

标签: bash amazon-web-services sed aws-cli

我正在尝试使用sed摆脱某些AWS命令输出中的几个可能的错误。

使用delete snapshots命令作为示例,这是我正在尝试使用的命令:

aws ec2 delete-snapshot --snapshot-id=snap-0f33096478a77c --profile=go
vcloud-admin-nonprod 2>&1 | sed -e 's/An error occurred (InvalidSnapshot.(\(NotFound|InUse|Malformed\)) when calling the DeleteSnapshot operation: //g'

错误1

An error occurred (InvalidSnapshotID.Malformed) when calling the DeleteSnapshot operation: Value ( snap-0f33096478a77c ) for parameter SNAPSHOT is invalid.

再次尝试使用其他错误:

aws ec2 delete-snapshot --snapshot-id=snap-02e5e9386fef2aaa9 --profile=govcloud-admin-nonprod 2>&1 | sed -e 's/An error occurred (InvalidSnapshot.(\(NotFound|InUse|Malformed\)) when calling the DeleteSnapshot operation: //g'

错误2:

An error occurred (InvalidSnapshot.NotFound) when calling the DeleteSnapshot operation: The snapshot 'snap-02e5e9386fef2aaa9' does not exist.

上次尝试输入其他错误:

aws ec2 delete-snapshot --snapshot-id=snap-0f33082478a77c --profile=go
    vcloud-admin-nonprod 2>&1 | sed -e 's/An error occurred (InvalidSnapshot.(\(NotFound|InUse|Malformed\)) when calling the DeleteSnapshot operation: //g'

错误3:

An error occurred (InvalidSnapshot.InUse) when calling the DeleteSnapshot operation: The snapshot snap-0f33096478a77c532 is currently in use by ami-d4128aae

我想要做的是除去最后一部分之外的所有文本,如果快照是否存在,等等。例如,而不是这个输出:

调用DeleteSnapshot操作时发生错误(InvalidSnapshot.NotFound):快照'snap-02e5e9386fef2aaa9'不存在。

我只想保留这部分:

The snapshot 'snap-02e5e9386fef2aaa9' does not exist.

由于某种原因,我使用的sed线并没有抑制我想要的输出。有人能用正确的语法帮助我完成这个吗?

我认为问题在于我如何形成这条线:

(InvalidSnapshot.(\(NotFound|InUse|Malformed\))

1 个答案:

答案 0 :(得分:1)

如果没有关于您的精确sed版本和操作系统平台的更多详细信息,这是一种温和的推测;但您尝试使用的正则表达式格式可能错误。要使用任何内容替换xa x(b),通常会使用以下语法:

sed -e 's/x\(a\|(b)\)//'
sed -E 's/x(a|\(b\))//'
sed -r 's/x(a|\(b\))//'

-E-r选项是非标准的,您的版本可能根本不存在。在它存在的地方,它选择扩展的(egrep)正则表达式语法而不是相当原始的传统sed正则表达式语法,这是POSIX调用基本正则表达式的变体。特别注意分组括号和交替栏如何在它们前面加一个反斜杠;没有反斜杠,他们只是简单地匹配自己。在ERE中,它们是正确的正则表达式元字符,你需要一个反斜杠转义来逐字匹配它们。

最后,请查看您当地的sed手册页。