views:

516

answers:

3

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.

Thank you! Ryan

+1  A: 

See the answers to my question: http://stackoverflow.com/questions/1574898/bash-and-filenames-with-spaces

Jim Garrison
+1  A: 
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
Awesome, this worked perfeclty. Thanks!!
purecharger
A: 

Edit # 2: Apologies. the code below is wrong. My attempts were on a local file, chosen randomly, that unluckily had no whitespace.

This breaks down a file into lines:

for i in `cat somefile.txt`; do echo "here is a single line:" $i ; done

Edit: I thought this example was clear enough on it's own, but I have been downvoted. I will be more clear. The echo statement is executed for each line in somefile.txt. That gives you the ability to process a file line by line. I have tested this in bash and zsh. If someone thinks this does not work, I'd be curious to know what they tried, and what the results were.

z5h
No, it doesn't.
mobrule
No, it doesnt work. Thank you though for posting.($:~/Projects/lvldirect-trunk/web)- for i in `cat send.jsp`; do echo "here is a single line:" $i ; donehere is a single line: <!DOCTYPEhere is a single line: htmlhere is a single line: PUBLIChere is a single line: "-//W3C//DTDhere is a single line: XHTMLhere is a single line: 1.0here is a single line: Transitional//EN"here is a single line: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">here is a single line: <htmlhere is a single line: xmlns="http://www.w3.org/1999/xhtml">
purecharger
Thank you both. Is it better to leave the comment above with edits explaining my mistake (for posterity)? Or deleting it altogether?
z5h