1

I have a template file like show below. I have a number of variables in it that I want to replace with values I peel off of a JSON doc. I'm able to do it with sed on the few simple ones, but I have problems doing it on <ARN> and others like that.

@test "Test <SCENARIO_NAME>--<EXPECTED_ACTION>" {
      <SKIP_BOOLEAN>
      testfile="data/<FILE_NAME>"

      assert_file_exist $testfile
      IBP_JSON=$(cat $testfile)

      run aws iam simulate-custom-policy \
          --resource-arns \
                          "<ARN>"
          --action-names \
                          "<ACTION_NAMES>"
          --context-entries  \
                          "ContextKeyName='aws:PrincipalTag/Service', \
                          ContextKeyValues='svc1', \
                          ContextKeyType=string"  \
                          "ContextKeyName='aws:PrincipalTag/Department', \
                          ContextKeyValues='shipping', \
                          ContextKeyType=string"  \
                          <EXTRA_CONTEXT_KEYS>
         --policy-input-list "${IBP_JSON}"

      assert_success
      <TEST_EXPRESSION>

}

I want the <ARN> placeholder to be replaced with the following text:

"arn:aws:ecs:*:588068252125:cluster/${aws:PrincipalTag/Service}-*"   \
"arn:aws:ecs:*:588068252125:task/${aws:PrincipalTag/Service}-*" \
"arn:aws:ecs:*:588068252125:container-instance/${aws:PrincipalTag/Service}-*"  \
"arn:aws:ecs:*:588068252125:task-definition/${aws:PrincipalTag/Service}-*:*"  \
"arn:aws:ecs:*:588068252125:service/${aws:PrincipalTag/Service}-*"  \

How can I do that replacement while also preserving the formatting (\ and /r at line ends)?

2 Answers 2

2

The easiest is use bash itself:

original=$(cat file.txt)
read -r -d '' replacement <<'EOF'
        "arn:aws:ecs:*:588068252125:cluster/${aws:PrincipalTag/Service}-*"   \
        "arn:aws:ecs:*:588068252125:task/${aws:PrincipalTag/Service}-*" \
        "arn:aws:ecs:*:588068252125:container-instance/${aws:PrincipalTag/Service}-*"  \
        "arn:aws:ecs:*:588068252125:task-definition/${aws:PrincipalTag/Service}-*:*"  \
        "arn:aws:ecs:*:588068252125:service/${aws:PrincipalTag/Service}-*"  \
EOF
placeholder='"<ARN>"'
modified=${original/$placeholder/$replacement}
echo "$modified"

Look for ${parameter/pattern/string} in man bash.

Sign up to request clarification or add additional context in comments.

Comments

0

If input.txt is the input file and replace.txt contains the replacement text:

$ cat input.txt 
      run aws iam simulate-custom-policy \
          --resource-arns \
                          "<ARN>"
          --action-names \
                          "<ACTION_NAMES>"

$ cat replace.txt 
"arn:aws:ecs:*:588068252125:cluster/${aws:PrincipalTag/Service}-*"   \\\
"arn:aws:ecs:*:588068252125:task/${aws:PrincipalTag/Service}-*" \\\
"arn:aws:ecs:*:588068252125:container-instance/${aws:PrincipalTag/Service}-*" \\\
"arn:aws:ecs:*:588068252125:task-definition/${aws:PrincipalTag/Service}-*:*" \\\
"arn:aws:ecs:*:588068252125:service/${aws:PrincipalTag/Service}-*" 

then you can use sed with @ delimiters to make the replacement:

$ sed "s@\"<ARN>\"@$(< replace.txt)@g" input.txt 
      run aws iam simulate-custom-policy \
          --resource-arns \
                          "arn:aws:ecs:*:588068252125:cluster/${aws:PrincipalTag/Service}-*"  \ 
"arn:aws:ecs:*:588068252125:task/${aws:PrincipalTag/Service}-*" \
"arn:aws:ecs:*:588068252125:container-instance/${aws:PrincipalTag/Service}-*" \
"arn:aws:ecs:*:588068252125:task-definition/${aws:PrincipalTag/Service}-*:*" \
"arn:aws:ecs:*:588068252125:service/${aws:PrincipalTag/Service}-*" 
          --action-names \
                          "<ACTION_NAMES>"

Here $(< replace.txt) is equivalent to $(cat replace.txt)

1 Comment

Thank you Sergio - I went with the simpler solution above, but thanks you for this new syntax above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.