Home > database >  Use sed to remove string, add whitespace and insert ""
Use sed to remove string, add whitespace and insert ""

Time:12-14

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?

CodePudding user response:

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 )

CodePudding user response:

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.

  • Related