[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Index function problem
From: |
Davide Brini |
Subject: |
Re: [bug-gawk] Index function problem |
Date: |
Sun, 21 Apr 2013 18:02:10 +0200 |
On Sun, 21 Apr 2013 08:06:33 +0100, Christopher Durant
<address@hidden> wrote:
> I notice that if you use a regular expression as the second parameter in
> an index call e.g. index ($0, /[A-Z]/); it is allowed but gives
> unpredictable results. Perhaps this usage should be flagged as an error.
awk converts /[A-Z]/ into 1 or 0 depending on whether $0 matches the
pattern. This number is then converted into a string, which is what index()
expect.
See these examples:
# this is like doing index($0, "0")
$ echo 'aBCD' | awk '{ print index($0, /[A-Z]/) }'
0
# this is like doing index($0, "1")
$ echo 'aB1D' | awk '{ print index($0, /[A-Z]/) }'
3
To do what you're likely trying to do, you need to use match() and the
special variables RSTART and RLENGTH. See the man for the details.
--
D.