diff options
Diffstat (limited to 'doc/gawk.texi')
-rw-r--r-- | doc/gawk.texi | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/doc/gawk.texi b/doc/gawk.texi index ec816c3b..3d90cd97 100644 --- a/doc/gawk.texi +++ b/doc/gawk.texi @@ -21653,6 +21653,36 @@ Because of this, you should not call it from an @code{ENDFILE} rule. (This isn't necessary anyway, because @command{gawk} goes to the next file as soon as an @code{ENDFILE} rule finishes!) +You need to be careful calling @code{rewind()}. You can end up +causing infinite recursion if you don't pay attenion. Here is an +example use: + +@example +$ @kbd{cat data} +@print{} a +@print{} b +@print{} c +@print{} d +@print{} e + +$ cat @kbd{test.awk} +@print{} FNR == 3 && ! rewound @{ +@print{} rewound = 1 +@print{} rewind() +@print{} @} +@print{} +@print{} @{ print FILENAME, FNR, $0 @} + +$ @kbd{gawk -f rewind.awk -f test.awk data } +@print{} data 1 a +@print{} data 2 b +@print{} data 1 a +@print{} data 2 b +@print{} data 3 c +@print{} data 4 d +@print{} data 5 e +@end example + @node File Checking @subsection Checking for Readable @value{DDF}s @@ -23349,7 +23379,7 @@ BEGIN @{ " for delimiter\n", Optarg) > "/dev/stderr" Optarg = substr(Optarg, 1, 1) @} - FS = Optarg + fs = FS = Optarg OFS = FS if (FS == " ") # defeat awk semantics FS = "[ ]" @@ -23371,7 +23401,12 @@ special care when the field delimiter is a space. Using a single space (@code{@w{" "}}) for the value of @code{FS} is incorrect---@command{awk} would separate fields with runs of spaces, TABs, and/or newlines, and we want them to be separated with individual -spaces. Also remember that after @code{getopt()} is through +spaces. +To this end, we save the original space character in the variable +@code{fs} for later use; after setting @code{FS} to @code{"[ ]"} we can't +use it directly to see if the field delimiter character is in the string. + +Also remember that after @code{getopt()} is through (as described in @ref{Getopt Function}), we have to clear out all the elements of @code{ARGV} from 1 to @code{Optind}, @@ -23522,8 +23557,8 @@ written out between the fields: @example @c file eg/prog/cut.awk -@{ - if (by_fields && suppress && index($0, FS) == 0) +{ + if (by_fields && suppress && index($0, fs) == 0) next for (i = 1; i <= nfields; i++) @{ @@ -24588,7 +24623,7 @@ BEGIN @{ if (! do_lines && ! do_words && ! do_chars) do_lines = do_words = do_chars = 1 - print_total = (ARGC - i > 2) + print_total = (ARGC - i > 1) @} @c endfile @end example |