0

I am trying to replace the below line in myfiles.hcl file

"-var-file=${get_parent_terragrunt_dir()}/variables/eu-west-2.tfvars", 

with

"-var-file=${get_parent_terragrunt_dir()}/variables/eu-west-2.tfvars",
"-var-file=${get_parent_terragrunt_dir()}/variables/eu-west-2-prod.tfvars",

using xargs gsed cmd

 find . -name 'myfiles.hcl' | xargs grep -l 'prod' | xargs gsed -i 's|"-var-file=${get_parent_terragrunt_dir()}/variables/eu-west-2.tfvars",|"-var-file=${get_parent_terragrunt_dir()}/variables/eu-west-2.tfvars",'\\n' "-var-file=${get_parent_terragrunt_dir()}/variables/eu-west-2-prod.tfvars",|'

But I am getting an error xargs: gsed: No such file or directory

1
  • 2
    And do you have a gsed command on your system? Commented Jun 9, 2022 at 6:10

1 Answer 1

1

You are probably on some non-GNU system like *BSD and your sed is written in a way that would not run on most sed flavours. So you can either install the GNU sed as gsed or you simply change the script to be compatible with your sed. I can see two GNU dependencies:

  1. The -i option is supported by many sed versions, but require an argument for backup file extension (a good idea because you can mess up your system easily with bulk inplace editing). So replace it by -i.bak.
  2. You use \n in the replacement, which is taken as a newline in GNU sed, but as the character n in most sed versions. The correct way is to use an actual newline preceeded by a blackslash.

Please allow me to give additional hints on your script:

  • Instead of repeating the whole matching pattern in the replacement, you could use &:

    sed 's/long pattern/& added after the match/'
    
  • Why do you leave the single quotes and do '\\n' instead of staying in the quotes and directly write \n?

  • Instead of adding a line by replacing the matching line by itself along with the new line, simply use the append command of sed:

    sed '/the line after which to append/a\
    the new line'
    
  • In your special case where the new line is almost the same as the given line, it could avoid errors by typos to print the line and modify it:

    sed -i.bak '\|"-var-file=${get_parent_terragrunt_dir()}/variables/eu-west-2.tfvars"|{p;s/west-2/&-prod/;}'
    

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.