6

How can I strip empty lines (surplus empy lines) from an input file using M4?

I know I can append dnl to the end of each line of my script to suppress the newline output, but the blank lines I mean are not in my script, but in a data file that is included (where I am not supposed to put dnl's).

I tried something like that:

define(`
',`')

(replace a new-line by nothing) But it didn't work.

Thanks.

j4x
  • 3,595
  • 3
  • 33
  • 64

2 Answers2

4

I use divert() around my definitions :

  • divert(-1) will suppress the output
  • divert(0) will restore the output

Eg:

divert(-1)dnl output supressed starting here

define(..)

define(..)

divert(0)dnl normal output starting here
use_my_definitions()...
drjors
  • 69
  • 1
  • 3
  • But wouldn't it suppress all the output, including what I want to be outputed? – j4x Mar 19 '13 at 20:04
  • everything after `divert(0)` will show up. your definitions are processed normally and can used after `divert(0)`. – drjors Apr 26 '13 at 18:28
1

I understand your problem to be a data file with extra line breaks, meaning that where you want to have the pattern data<NL>moredata you have things like data<NL><NL>moredata.

Here's a sample to cut/paste onto your command line that uses here documents to generate a data set and runs an m4 script to remove the breaks in the data set. You can see the patsubst command replaces every instance of one or more newlines in sequence (<NL><NL>*) with exactly one newline.

cat > data << -----
1, 2
3, 4

5, 6

7, 8



9, 10
11, 12
e
-----

m4 << "-----"
define(`rmbreaks', `patsubst(`$*', `

*', `
')')dnl
rmbreaks(include(data))dnl
-----
bk.
  • 6,068
  • 2
  • 24
  • 28