Replacing lines in text files

Hi,

I have 2 sets of text files. I need to take a field from a certain line in set 1 and put it in the same place in set b. The line appears once per file, in different places but is a set format and has the unique word "ANTENNA" in it and is always 81 characters long. Example from set a:

" 0.0000 0.0000 0.0000 ANTENNA: DELTA H/E/N"
Example from set b
" 0.2160 0.0000 0.0000 ANTENNA: DELTA H/E/N"

The bold bit is the field I'm trying to change and will be a random float in both sets.

I've been trying to do it like this:

#!/bin/ksh

old_line=`grep ANTENNA ./file1.txt`
new_line=`grep ANTENNA ./file2.txt`

sed "s/${old_line}/${new_line}/" file1.txt >tmp2 #substitute old line with new line in file 1 and output to tmp2

But this doesn't work, probably because I have forward slashes in my grep lines which sed interprets as some sort of regexp. How can I escape these when they are embedded in a variable? Am I quoting incorectly? Or is there a much better way?

Jon

sed "s\/${old_line}\/${new_line}\/" f

use escape sequence "\" and then try.

This results in the error:

sed: -e expression #1, char 81: unknown option to `s'

sed "s#${old_line}#${new_line}#" file1.txt

Much appreciated.

But if you have # in the lines you will get the same problem. Does sed not have a quotemeta function/option you can use for more general purposes and not have to worry about the specific delimiter in the search expression?

not that I'm aware of......

Looks like you are correct, but you could code your own "quotemeta" function by having a regular expression put a backslash in front of all non word characters (except maybe $) in the search string before sending it off to the regular expression for parsing.

But then again, if the user knows the delimiter '/' is in the search string and '#' (or whatever) is not, they may as well just use the solution provided as it is much simpler.

\Q is a handy and important part of perls regexp arsenal as this problem is quite common.