Taking your questions one at a time:
- How do the
~
and match()
operators interact with each other?
They don't. At least not directly in your code. ~
is the regexp comparison operator. In the context of $0 ~ ENVIRON["search"]
it is being used to test if the regexp contained in the environment variable search
exists as part of the current record ($0
). If it does then the code in the subsequent {...}
block is executed, if it doesn't then it isn't.
- How does the
match()
have any effect, if its output isn't printed or
echoed?
It identifies the starting point (and stores it in the awk variable RSTART
) and the length (RLENGTH
) of the first substring within the first parameter ($0
) that matches the regexp provides as the second parameter (id=[0-9]+
). With GNU awk it can also populate a 3rd array argument with segments of the matching string identified by round brackets (aka "capture groups").
- What does it actually return or do?
It returns the value of RSTART
which is zero if no match was found, 1 or greater otherwise. For what it does see the previous answer.
- How can I use it in my own code?
As shown in the example you posted would be one way but that code would more typically be written as:
($0 ~ ENVIRON["search"]) && match($0,/id=[0-9]+/) {
print substr($0, RSTART+3, RLENGTH-3)
}
and using a string rather than regexp comparison for the first part would probably be even more appropriate:
index($0,ENVIRON["search"]) && match($0,/id=[0-9]+/) {
print substr($0, RSTART+3, RLENGTH-3)
}
Get the book Effective Awk Programming, 4th Edition, by Arnold Robbins to learn how to use awk.