DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
m4 macro processor

String manipulation

The len macro returns the length of the string (number of characters) in its argument.

   len(abcdef)
is 6, and
   len((a,b))
is 5.

The substr macro can be used to produce substrings of strings. If you type

substr(s, i, n)

it will return the substring of s that starts at the ith position (origin 0) and is n characters long. If n is omitted, the rest of the string is returned. If you type

   substr(`now is the time',1)
it will return the following string:
   now is the time
If i or n are out of range, a blank line is returned. For example, if you type
   substr(`now is the time',-1)
or
   substr(`now is the time',1,39)
you will get a blank line.

The index(s1, s2) macro returns the index (position) in s1 where the string s2 occurs, -1 if it does not occur. As with substr, the origin for strings is 0.

translit performs character transliteration and has the general form

   translit(s, f, t)
which modifies s by replacing any character in f by the corresponding character in t. Using input
   translit(s, aeiou, 12345)
replaces the vowels by the corresponding digits. If t is shorter than f, characters that do not have an entry in t are deleted. As a limiting case, if t is not present at all, characters from f are deleted from s.
   translit(s, aeiou)
would delete vowels from s.

The macro dnl deletes all characters that follow it up to and including the next new-line. It is useful mainly for throwing away empty lines that otherwise would clutter up m4 output. Using input

   define(N, 100)
   define(M, 200)
   define(L, 300)
results in a new-line at the end of each line that is not part of the definition. The new-line is copied into the output where it may not be wanted. When you add dnl to each of these lines, the new-lines will disappear. Another method of achieving the same result is to input
   divert(-1)
   define(...)
    ...
   divert

Next topic: Printing
Previous topic: Conditionals

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005