linuxunixlambdabase64

How to base64 decode a sed output using piping


I have a lambda that I want to invoke using CLI. The LogResult in the output is base64 encoded data. I am trying to fetch the log data in "LogResult", remove the preceding and following double quotes, and eventually decode it. I am currently doing it using separate commands

$ aws lambda invoke --function-name test-lambda-function out --log-type Tail --query "LogResult" > output.log

$ sed -i 's/\"//g' output.log

$ base64 --decode output.log

But this seems needlessly verbose. I've been trying to pipe these commands but something is obviously wrong. I've tried the following

$  aws lambda invoke --function-name test-lambda-function out --log-type Tail --query "LogResult" | sed -i 's/\"//g' | base64 --decode

sed: unknown option -- decode

$  aws lambda invoke --function-name test-lambda-function out --log-type Tail --query "LogResult" | tr -d '"' | base64 --decode

tr: extra operand '|'

What's the neatest way to achieve what I want to do?


Solution

  • The cleanest way to combine these commands is by using command substitution and piping. The main issue in your attempts is mixing up the sed and tr commands with | pipes in the wrong context.

    Here’s a one-liner that should do exactly what you want:

    aws lambda invoke --function-name test-lambda-function out --log-type Tail --query "LogResult" --output text | tr -d '"' | base64 --decode

    Here’s how it works:

    aws lambda invoke ... --output text returns the output as plain text without the surrounding quotes.
    tr -d '"' removes any remaining double quotes if necessary.
    base64 --decode decodes the Base64 string to show the actual log content.
    

    Explanation of the issues:

    Using --output text with the AWS CLI removes the surrounding quotes automatically, so you don't need sed in this pipeline.
    The | pipe is used correctly here to pass the result of each command to the next in sequence, without attempting to edit files directly.