bluethundr
bluethundr

Reputation: 1345

Remove multiple AWS Errors with sed

I'm trying to get rid of several possible errors in the output of some AWS commands using sed.

Using a delete snapshots command as an example, here's the command I'm trying to use:

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'

Error 1

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

Trying again with a different error:

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'

Error 2:

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

Last try for a different error:

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'

Error 3:

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

What I'm trying to do is get rid of all text except for the last part that says if the snapshot exists or not, etc. For example instead of this output:

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

I just want to preserve this part:

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

For some reason the sed line I'm using doesn't suppress the output I want. Can someone help me with the right syntax to accomplish this?

I think the problem lies in how I'm forming this line:

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

Upvotes: 0

Views: 452

Answers (1)

tripleee
tripleee

Reputation: 189789

Without more details about your precise sed version and OS platform, this is mildly speculative; but the regex format you are trying to use is probably wrong. To replace xa or x(b) with nothing, the following syntaxes commonly work:

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

The -E or -r option is nonstandard, and may not exist at all in your version. Where it does exist, it selects extended (egrep) regex syntax instead of the rather primitive traditional sed regex syntax, which is a variant of what POSIX calls basic regular expressions. Notice in particular how grouping parentheses and the alternation bar need a backslash in front of them; without the backslash, they simply match literally themselves. In ERE they are proper regex metacharacters, and you need a backslash escape to match them verbatim.

Ultimately, check your local sed manual page.

Upvotes: 1

Related Questions