How to match newlines in GNU M4 _properly_
Asked Answered
P

1

1

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?

Prologize answered 20/6, 2010 at 9:58 Comment(0)
F
2

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++
++
Fenner answered 3/8, 2010 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.