manpagez: man pages & more
info gawk
Home | html | info | man

File: gawk.info,  Node: Leftmost Longest,  Next: Regexps.php">Computed Regexps,  Prev: Bracket Expressions,  Up: Regexp

3.5 How Much Text Matches?
==========================

Consider the following:

     echo aaaabcd | awk '{ sub(/a+/, ""); print }'

   This example uses the 'sub()' function to make a change to the input
record.  ('sub()' replaces the first instance of any text matched by the
first argument with the string provided as the second argument; *note
String Functions::.)  Here, the regexp '/a+/' indicates "one or more 'a'
characters," and the replacement text is ''.

   The input contains four 'a' characters.  'awk' (and POSIX) regular
expressions always match the leftmost, _longest_ sequence of input
characters that can match.  Thus, all four 'a' characters are replaced
with '' in this example:

     $ echo aaaabcd | awk '{ sub(/a+/, ""); print }'
     -| bcd

   For simple match/no-match tests, this is not so important.  But when
doing text matching and substitutions with the 'match()', 'sub()',
'gsub()', and 'gensub()' functions, it is very important.  *Note String
Functions::, for more information on these functions.  Understanding
this principle is also important for regexp-based record and field
splitting (*note Records::, and also *note Field Separators::).

© manpagez.com 2000-2025
Individual documents may contain additional copyright information.