Bash 将aws cli输出中的行替换为sed

Bash 将aws cli输出中的行替换为sed,bash,sed,Bash,Sed,我有一个脚本,我写的删除AWS快照。我想在快照不存在时删除部分输出 在此输出中: Deleting unused snapshots in AWS Gov Non Prod ***************************************************************** deleting snapshot: snap-0b571b64784bac904 An error occurred (InvalidSnapshot.NotFound) when call

我有一个脚本,我写的删除AWS快照。我想在快照不存在时删除部分输出

在此输出中:

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.
*****************************************************************
我只想删除短语:“调用DescribeSnapshots操作时发生错误(InvalidSnapshot.NotFound)”,并保留短语:快照“snap-0b571b64784bac904”不存在

在我的脚本中,我尝试添加sed以删除我不想要的内容:

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

    #!/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

因此,基本上sed线路正在失败。如何使用sed删除我不想要的部分?

编辑:删除OP使用的第一个改进

这个问题不应该那么难,但我们必须分步尝试。 您按照我的建议添加了
2>/dev/null
,但写在了错误的地方:

aws ... --profile=lab | sed -e 's/...//g' 2>/dev/null
应该是

aws ... --profile=lab 2>/dev/null | sed -e 's/...//g' 
当这项工作,你将错过整个行,所以更好的是

aws ... --profile=lab 2>&1 | sed -e 's/...//g' 
如果这没有帮助,请尝试以下方法:

查看您的代码以及您在代码中编写的内容
示例输出显示在AWS Gov Non Prod中删除未使用的快照,代码显示“在AWS实验室中删除未连接的卷”
您正在测试您正在编辑的同一个文件吗

确保
#/bin/bash
位于第一行,没有额外空格

使用临时文件进行调试
将aws输出重定向到一个文件并查看该文件

aws ... --profile=lab > /tmp/aws.out
vi /tmp/aws.out
(查找或特殊字符、制表符等)

使用/tmp/aws.out尝试简单命令

sed 's/An error/===/' /tmp/aws.out
sed 's/An error.*: /===/' /tmp/aws.out # something special in the part you try to match
sed 's/ /=/' /tmp/aws.out # Are all white spaces a space ?
sed 's/.*/===/' /tmp/aws.out # Can sed do anything at all or are you missing an EOL 
grep -v "An error" /tmp/aws.out # sed should be fine, but will this remove the complete line?

但是,请确保您没有需要转义的字符、点“.”以及可能需要在sed搜索字符串中特别注意的parens“()”;在这种情况下,似乎不需要搜索大型特定字符串。试着像这样简化它,然后增加字符串的复杂性。这在我的测试中起作用,我将一个文件cat到stdout,但是您的命令可能会发送到stderr,如果是这样,第二行应该适合您

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.*: //'

在你发布的输入片段上运行良好!对我来说根本不起作用。请尝试将aws输出重定向到文件,然后在其上运行
file
file awsOutput
。如果它没有说
awsOutput:ASCII text
,那可能是您的问题。我真的看不出还有什么地方会出问题,除非你有sed别名或sed的奇怪版本
alias sed
sed--version
来检查这些东西。好的,谢谢。当我对输出执行文件操作时,确实会得到
awsOutput.txt:ASCII文本。我正在Ubuntu 16.04中使用SED4.2.2。sed没有别名。嗯,在调用DescribeSnapshots操作:\s*?//g'
时,您可以尝试使用
sed-re/error\(InvalidSnapshot\.NotFound\),因为最后一位空白实际上是一个制表符。看起来不太可能,谢谢!我根据你的观察修正了密码。使用sed仍然无法获得期望的结果。即使在逐字复制建议行时。示例中的代码现在与脚本的当前形式相同。如有任何建议,将不胜感激!是否将一部分写入stderr(添加
2>/dev/null
后会发生什么情况)?感谢您的建议!不幸的是,写信给stderr并没有奏效。我再次更新了OP中的代码,以显示当前的脚本。谢谢!添加行:
aws--profile=lab2>&1 | sed-e's/../g'
让它完全完成了我需要它做的事情。非常感谢你的贡献!我认为这可能是在stderr上输出数据的第二种情况