How to use awk variables in regular expressions?

前端 未结 5 1460
渐次进展
渐次进展 2020-12-03 09:52

I have a file called domain which contains some domains. For example:

google.com
facebook.com
...
yahoo.com

And I have ano

5条回答
  •  醉梦人生
    2020-12-03 10:15

    The problem of the answers above is that you cannot use the "metacharacters" (e.g. \< for a word boundary at the beginning of a word) if you use a string instead of a regular expression /.../. If you had a domain xyz.com and two sites ab.xyz.com and cd.prefix_xyz.com, the numbers of the two site entries would be added to xyz.com

    Here's a solution using awk's pipe and the sed command: ...

    for(dom in domain) {
        while(getline < "./site" > 0) {
            # let sed replaces occurence of the domain at the end of the site
            cmd = "echo '" $1 "' | sed 's/\\<'" dom "'$/NO_VALID_DOM/'"
            cmd | getline x
            close(cmd)
            if (match(x, "NO_VALID_DOM")) { 
              domain[dom]+=$2;
            }
        }
        close("./site") # this misses in original code
    }
    

    ...

提交回复
热议问题