New User Looking for help with a shell script.

Greetings all.

I have come across a problem that I am struggling to solve (correctly).

I am looking to write a script that does the following.

  1. I have a file that contains the follwing.

file1
#REPLACE_1

some written text...

#REPLACE_2

some more text

  1. I then have 2 file that contain lines that should replace the tags.

File REPLACE_1 contains
LINE1

LINE2

File REPLACE_2 contans
LINE3

LINE4

My problem is that I cannot get the whitespaces/newlines to be included in the merged file. Any ideas would be most appreciated as it is starting to drive me round the bend!! :eek:

So to sum up the resulting file should look like this:

RESULTS FILE
LINE1

LINE2

some written text...

LINE3

LINE4

some more text

I thank you all in advance.

Si :b:

Forgot to say. That at present there are never any whitespaces/newlines. So the followng code I wrote work fine for what I need.

Go easy on it as I am no expert. Current solution is:

grep -i ^#REPLACE_ $file > ./tmpreplace

while read replace
do

awk ' BEGIN { RS="" }
FILENAME==ARGV[1] { r=$0; s=FILENAME }
FILENAME==ARGV[2] { sub(s,r) ; print }
' $replace $file > $tfile

file=$tfile
cnt=$cnt+1
tfile="TMP"$cnt

done < ./tmpreplace

awk '/^#REPLACE/ { 
  x = $0
  while ((getline < ("REPLACE_"(x ~ /1/?"1":"2"))) > 0 )
    print
    next
}1' file1

Use nawk or /usr/xpg4/bin/awk on Solaris.

That is just the ticket. Tip of the hat

Now I just have to try and understand it!! :confused:

The following line:

while ((getline < ("REPLACE_"(x ~ /1/?"1":"2"))) > 0 )

Is perfect for the example I gave. However the only static information is #REPLACE_. This could be for example #REPLACE_DEFS #REPLACE_WORK etc...

So they are not confined to numbers. I guess that what the 1 and 2 is on the above statement (eg the example I gave.)

Is it possible to make this fluid? :confused: I guess by maybe doing another loop looking for the full replace statement?

I will play around and give it a go.

But again thank you very much for putting me in the right direction.

:):b:

PS. However the filename are ALWAYS the same as the REPLACE tags.

I knew it, but I didn't resist :slight_smile:
OK, try this:

awk '/^#REPLACE/ {
  f = $2
  while ((getline < f) > 0)
    print
    next
}1' FS="#" file1

Use nawk or /usr/xpg4/bin/awk on Solaris.

You are indeed an addict!!! :smiley:

Thank you so so much. :b:

It generally doesn't work because the second program ("sub(s,r); print") is not executed until all the lines of the first file are consumed. The problem with whitespace has to do with your shell-read command, which ignores whitespace.

Ok. About to scream:eek:

Tried not to come back and look stupid, but the fact of the matter is "I AM!"

So the script radoulov gave is perfect.

Except that it does not more than once if the tag appear xtimes in file1 I only see it once then then same tags get replaced with a whitespace??:confused:

anyone, as I will be honest and say I dont fully understand what radoulov wrote in the first place:(

Thanks in advance :b:

Lets fix it in the first place :slight_smile:

awk '/^#REPLACE/ {
  f = $2
  while ((getline < f) > 0)
    print
    close(f)
    next
}1' FS="#" file1