when searching treat line as string, and regex as regexp

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
Steven Armstrong 2018-05-18 16:57:34 +02:00
parent a3968f8313
commit fb26894cbd
2 changed files with 18 additions and 4 deletions

View File

@ -37,6 +37,13 @@ else
fi
awk -v position="$position" -v needle="$needle" '
function _find(_text, _pattern) {
if (needle == "regex") {
return match(_text, _pattern)
} else {
return index(_text, _pattern)
}
}
BEGIN {
getline anchor < (ENVIRON["__object"] "/parameter/" position)
getline pattern < (ENVIRON["__object"] "/parameter/" needle)
@ -46,7 +53,7 @@ BEGIN {
if (position == "after") {
if (match($0, anchor)) {
getline
if (match($0, pattern)) {
if (_find($0, pattern)) {
state = "present"
}
else {
@ -56,7 +63,7 @@ BEGIN {
}
}
else if (position == "before") {
if (match($0, pattern)) {
if (_find($0, pattern)) {
getline
if (match($0, anchor)) {
state = "present"
@ -68,7 +75,7 @@ BEGIN {
}
}
else {
if (match($0, pattern)) {
if (_find($0, pattern)) {
state = "present"
exit 0
}

View File

@ -78,6 +78,13 @@ if [ -f "$file" ]; then
fi
awk -v position="$position" -v needle="$needle" -v remove=$remove -v add=$add '
function _find(_text, _pattern) {
if (needle == "regex") {
return match(_text, _pattern)
} else {
return index(_text, _pattern)
}
}
BEGIN {
line_file = ENVIRON["__object"] "/parameter/line"
getline line < line_file
@ -88,7 +95,7 @@ BEGIN {
}
{
if (remove) {
if (match(\$0, pattern)) {
if (_find(\$0, pattern)) {
# skip over this line -> remove it
next
}