[awk] String function - match
Object 
- How to use match functions with Awk.
Syntax
match(String, regexp[, array])
Definition
- The match function searches String for the longest, leftmost substring matched by the regular expression.
- It returns the character position, or index, ah which that substring begins.
You have a file below. (subject.txt)
i love you
i love us
i love her
i love him
i love them
match.awk
BEGIN{
        OFS=" "
        print "How many lines do the file match the regexp"
        cnt=0;
}
{
        regexp = "h.+";
        pos = match($0, regexp);
        if( pos != 0 ){
                print $1,$2,$3,"[pos:"pos"]"
                cnt++;
        }
}
END{
        print "Total Count:",cnt
}
Execute Awk command with files above.
$>awk -f match.awk subject.txt
Result
How many lines do the file match the regexp
i love her [pos:8]
i love him [pos:8]
i love them [pos:9]
Total Count: 3
Referred Links
 
댓글
댓글 쓰기