Search code examples
regexm4

How to add a prefix to each line with m4


I process a program file (in fact assembler) with m4. I can suppress lines using

changequote({,})dnl
define({_SUPPRESSED}, )dnl
_SUPPRESSED({
jan
piet
})

Now I want to changes the lines to assembler comment instead of just suppressing them:

; jan
; piet

Is that possible with m4 e.g. using patsubst? How can it be done?

Note: Despite much experimenting I didn't manage. E.g. the following fails.

define({pie}, {patsubst($1,{\n},{\n;})})

Solution

  • define(`pie',`patsubst(`$1',`^.*$',`; &')')
    pie(`jan
    piet')
    

    Note the string delimiters around patsubst (in pie's definition)! Without it the m4 expands the patsubst at reading (not at using) pie's definition. With delimitiers it will be a string and expand only when call pie macro.

    Of course you can use { and } as string delimitiers.