Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU-M4: Strip empty lines

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.

like image 492
j4x Avatar asked Dec 12 '12 15:12

j4x


2 Answers

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()...
like image 101
drjors Avatar answered Nov 15 '22 06:11

drjors


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
-----
like image 28
bk. Avatar answered Nov 15 '22 05:11

bk.