1

I have a file with the following entry:

export TF_VAR_environment_name=dev
export TF_VAR_project_name=hello-world

I would like to do 3 things with these enteries:

  1. Remove the export TF_VAR_ string
  2. Add whitespace to both sides of =
  3. Wrap the string right of = in " "

So my file would end up looking like:

environment_name = "dev"
project_name = "hello-world"

I'm able to remove the string with s/"export TF_VAR_"//, but haven't been able to wrap the = in whitespace, or wrap the final string in quotes. Any help would be greatly appriciated.

Is this possible to do in sed?

1
  • Most probably yes, please post what have you tried so far? Where did it failed? Commented Dec 13, 2022 at 16:04

2 Answers 2

2

input.txt is your textfile. output.txt is the wanted result.

sed 's/export TF_VAR_// ; s/=\ (.*\ )$/ = "\1"/ ' < input.txt > output.txt

there is no blank between \ and (
and no blank between \ and )

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

4 Comments

Thank you for the response. This currently gives the following error: sed: -e expression #1, char 39: invalid reference \1 on `s' command's RHS Anything in the syntax looking incorrect?
Add option -E.
I'm using macos (m1), and also testing in sed.js.org Both have the error I mentioned above
-E flag worked, thank you @Cyrus
0

input.txt is your textfile. output.txt is the wanted result.

sed 's/export TF_VAR_// ; s/=\(.*\)$/ = "\1"/ ' < input.txt > output.txt

its the same as above. i just tried to post it here in a correct way.

1 Comment

You can just edit your original answer. If you don't have 2 distinct answers, you should always try to edit the original answer when adding more information or editing formatting :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.