Search code examples
newlinem4

How to match newlines in GNU M4 _properly_


I am trying to craft a macro replacing newlines.

My first try was:

    define(`m4_pascal_str',`
     patsubst(`$1',`^\(.*\)$',`\1++')
')

m4_pascal_str(`

11

22 33 44
')

define(zz,`

11

22 33 44
')
m4_pascal_str(`zz')

That gives correct answer when not using intermediate macro, and match only last newline otherwise. See results below:

 ++

++
11++
++
22 33 44++

++

11

22 33 44
++

Then I found similar question: in m4's patsubst, how do I replace newlines with spaces?

So, I just made:

define(`m4_pascal_str',`
     patsubst(`$1',`
',`++')
')

m4_pascal_str(`

11

22 33 44
')

define(zz,`

11

22 33 44
')
m4_pascal_str(`zz')

It gives:

 ++++11++++22 33 44++

11

22 33 44

The last alternative suffers the same problem. Any suggestions?


Solution

  • For the last line try removing the quoting around the zz. When I did this I got the same result for both m4_pascal_str calls:

         ++
    ++
    11++
    ++
    22 33 44++
    ++
    
    
    
    
         ++
    ++
    11++
    ++
    22 33 44++
    ++