Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace line in aws cli output with sed

Tags:

bash

sed

I have a script that I wrote that deletes AWS snapshots. I want to remove part of the output when a snapshot doesn't exist.

In this output:

Deleting unused snapshots in AWS Gov Non Prod
*****************************************************************
deleting snapshot: snap-0b571b64784bac904

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


*****************************************************************
Verify that snapshot: snap-0b571b64784bac904 is gone:

An error occurred (InvalidSnapshot.NotFound) when calling the DescribeSnapshots operation: The snapshot 'snap-0b571b64784bac904' does not exist.
*****************************************************************

I want to remove just the phrase: "An error occurred (InvalidSnapshot.NotFound) when calling the DescribeSnapshots operation:" and leave the phrase: The snapshot 'snap-0b571b64784bac904' does not exist.

In my script I tried adding sed to remove what I don't want:

aws ec2 delete-snapshot --snapshot-id=$i --profile=govcloud-nonprod | sed -e 's/An error occurred (InvalidSnapshot.NotFound) when calling the DeleteSnapshot operation: //g'

This is my script in its entirety:

    #!/bin/bash

echo "Deleting unattached volumes in AWS Lab"

for i in $(cat aws_lab_volumes.txt)
do
  echo "*****************************************************************"
  echo "deleting volume: $i"
  aws ec2 delete-volume --volume-id=$i --profile=lab | sed -e 's/An error occurred (InvalidVolume.NotFound) when calling the DeleteVolume operation: //g' 2>/dev/null
  echo "*****************************************************************"
  echo; echo; echo; echo; echo
  sleep 5
  echo "*****************************************************************"
  echo "Verify that volume: $i is gone:"
  aws ec2 describe-volumes --volume-ids=$i --profile=lab | sed -e 's/An error occurred (InvalidVolume.NotFound) when calling the DescribeVolumes operation: //g' 2>/dev/null
  echo "*****************************************************************"
  echo; echo; echo; echo; echo
done

So basically the sed line is failing. How can I use sed to remove just the part I don't want?

like image 449
bluethundr Avatar asked May 25 '26 00:05

bluethundr


1 Answers

Make sure you don't have a character that requires an escape, the dot "." and likely the parens "()" are going to need special attention in the sed search string, however; In this case there doesn't seem to be a need for large specific strings to search. Try dumbing it down like so and increase string complexity from there. This worked in my tests which I cat a file to stdout, however your command may send to stderr if so the second line should work for you.

aws ec2 delete-snapshot --snapshot-id=$i --profile=govcloud-nonprod | sed 's/.*DescribeSnapshots.*: //'

aws ec2 delete-snapshot --snapshot-id=$i --profile=govcloud-nonprod 2>&1 | sed 's/.*DescribeSnapshots.*: //'
like image 164
Josiah DeWitt Avatar answered May 27 '26 13:05

Josiah DeWitt