1

I'm writing a script to do variable substitution into a Java properties file, of the format name=value. I have a source file, source.env like this:

TEST_ENV_1=test environment variable one
TEST_ENV_2=http://test.environment.com/one
#this is a comment with an equal sign=blah
TEST_ENV_3=/var/log/test/env/2.log

My script will replace every occurence of TEST_ENV_1 in the file dest.env with "test environment variable one", and so on.

I'm trying to process a line at a time, and having problems because looping on output from a command like sed or grep tokenizes on white space rather than the entire line:

$ for i in `sed '/^ *#/d;s/#.*//' source.env`; do
  echo $i
  done
TEST_ENV_1=test
environment
variable
one
TEST_ENV_2=http://test.environment.com/one
TEST_ENV_3=/var/log/test/env/2.log

How do I treat them as lines? What I want to be able to do is split each line apart on the "=" sign and make a sed script with a bunch of substitution regex's based on the source.env file.

hakre
  • 193,403
  • 52
  • 435
  • 836
purecharger
  • 1,213
  • 2
  • 15
  • 34

1 Answers1

5
sed '/^ *#/d;s/#.*//' source.env | while read LINE; do
  echo "$LINE"
done

An alternative is to change $IFS as per @Jim's answer. It's better to avoid backticks in this case as they'll cause the entire file to be read in at once, whereas piping the output of sed to while above will allow the file to be processed line by line without reading the whole thing in to memory.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578