ady8531
ady8531

Reputation: 678

replace string in a file with a string from within the same file

I have a file like this (tens of variables) :

PLAY="play"
APPS="/opt/play/apps"
LD_FILER="/data/mysql"
DATA_LOG="/data/log"

I need a script that will output the variables into another file like this (with space between them):

PLAY=${PLAY} APPS=${APPS} LD_FILER=${LD_FILER}

Is it possible ?

Upvotes: 1

Views: 33

Answers (3)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed 's/"\([^"]*"\)"/={\1}/;H;$!d
x;y/\n/ /;s/.//' YourFile

your sample exclude last line so if this is important

sed '/DATA_LOG=/ d
   s/"\([^"]*"\)"/={\1}/;H;$!d
   x;y/\n/ /;s/.//' YourFile

Upvotes: 0

William Pursell
William Pursell

Reputation: 212684

< input sed -e 's/\(.*\)=.*/\1=${\1}/' | tr \\n \ ; echo

Upvotes: 1

fedorqui
fedorqui

Reputation: 290525

I would say:

$ awk -F= '{printf "%s=${%s} ", $1,$1} END {print ""}' file
PLAY=${PLAY} APPS=${APPS} LD_FILER=${LD_FILER} DATA_LOG=${DATA_LOG} 

This loops through the file and prints the content before = in a format var=${var} together with a space. At the end, it prints a new line.

Note this leaves a trailing space at the end of the line. If this matters, we can check how to improve it.

Upvotes: 3

Related Questions