use m4 as a replacement for sed

"Gerhard Lausser" <[email protected]>
Newsgroups gmane.comp.gnu.m4.general
Message-ID <[email protected]>
Hi List,

i want to use m4 to replace certain substrings in a text file.

My file looks like this (a Nagios configuration file):

define service {
    register               0
    name                   app_mssql_DBSRV
    host_name              HOSTNAME
}

What i want to do is to replace DBSRV and HOSTNAME which is simple with sed:

$ cat sample.cfg | sed -e 's/DBSRV/CMQS/g' -e 's/HOSTNAME/dbsrv11/g'
define service {
    register               0
    name                   app_mssql_CMQS
    host_name              dbsrv11
}

Alas, i like the definition of macros on the command line with m4. I think
this would be easier to script.

$ cat sample.cfg | m4 -DDBSRV=CMQS -DHOSTNAME=dbsrv11
define service {
    register               0
    name                   app_mssql_DBSRV
    host_name              dbsrv11
}

Unfortunately i found no way to make the DBSRV-substring in app_mssql_DBSRV
be recognized as a macro.
I played with "app_mssql_(DBSRV)" and it worked, but the delimiters appeared
in the output so i had to add a sed-command which replaced (.*) with .* .The
other thing is...i don't want to add too much extra code to the input file. 
What i understand is, DBSRV as a substring is not a macro and will me
ignored by m4.
Maybe it's impossible at all but maybe someone knows a simple trick how to
detect a macro name embedded in another string.

Cheers,
Gerhard
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.