regexawkgawk

Capture caret (^) with an awk regex


I have output with this format:

/ignore-this/^/../I/want/this@ignore-this

I am trying to use an awk regex to capture the following:

../I/want/this

This wouldn't be particularly hard except that I cannot figure out how to properly escape the ^ so it is not interpretted as an new line or a not. Below is what I have so far, it almost works except it prints out:

/ignore-this/^/../I/want/this

Here is the code:

#!/bin/awk -f                                                                              
{
    if (match($0, "\^.*@")){
        print substr($0, RSTART, RLENGTH-1);
    }
}

Solution

  • Another possibility, using gawk:

    #!/opt/local/bin/gawk -f
    {
        if (match($0, /[\^]\/(.*)@/, pieces)) {
            print pieces[1];
        }
    }