String matching: Difference between revisions

awk cannot use ~ unless sanitized string, use index instead
(awk cannot use ~ unless sanitized string, use index instead)
Line 998:
<lang AWK>#!/usr/bin/awk -f
{
print $1 " " (index($2,$1)==1 ? "begins" : "does not begin" ) " with " $2;
if ($1 ~ "^"$2) {
print $1 " " (index($2,$1) ? "contains" : "does not contain" ) " " $2;
print $1" begins with "$2;
print $1 " " ( substr($2,index($2,$1))==$1 ? "ends" : "does not end") " with " $2
} else {
print $1" does not begin with "$2;
}
 
if ($1 ~ $2) {
print $1" contains "$2;
} else {
print $1" does not contain "$2;
}
 
if ($1 ~ $2"$") {
print $1" ends with "$2;
} else {
print $1" does not end with "$2;
}
}
</lang>
Anonymous user