JamesD
JamesD

Reputation: 719

How do I replace template variables in text file with data in bash script

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)?

Upvotes: 1

Views: 491

Answers (2)

builder-7000
builder-7000

Reputation: 7627

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)

Upvotes: 0

Amadan
Amadan

Reputation: 198324

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.

Upvotes: 2

Related Questions