我试图在sed中找到要使用的正则表达式,并将模式(可能包含嵌套参数)包含在一些文本中。
一个基本的例子是
length(bill_cycle)
判断器应该
length(cast(bill_cycle as string))
在这里,我们从length(
开始搜索它,并找到与length(
相关的关闭)
。然后我们用cast(bill_cycle as string)
替换介于两者之间的任何东西,在本例中,用D6
替换
即使变量(在本例中为some(somethiing)
)具有嵌套参数,如
length(some(somethiing))
判断器应该
length(cast(some(somethiing) as string))
我对任何unix脚本或其他能够工作的命令都是开放的。任何帮助都是非常感谢的。
发布于 2020-10-02 10:13:43
Perl来救我!
perl -MText::Balanced=extract_bracketed \
-ne 'if (/length(\(.*)/) {
($arg) = (extract_bracketed(($1 =~ /\((.*)\)/)[0]))[1];
print "length(cast($arg as string))\n";
} else { print }' -- input.file > output.file
它使用核心模块案文:平衡从字符串中提取带有平衡分隔符的子字符串。
发布于 2020-10-02 11:07:25
使用perl
和递归匹配:
$ cat ip.txt
length(bill_cycle)
length(some(somethiing))
$ perl -pe 's/length(\(((?:[^()]++|(?1))++)\))/length(cast($2 as string))/' ip.txt
length(cast(bill_cycle as string))
length(cast(some(somethiing) as string))
请参阅https://www.rexegg.com/regex-recursion.html了解递归是如何工作的。
发布于 2020-10-02 20:29:47
下面是一个awk脚本,它不对括号使用模式匹配,但对它们进行计数。它还将匹配一个以上的每一行发生。
BEGIN {
p = "length"
}
{
row = $0
while (row ~ p"\\(") {
# get the substring from pattern to the end of the line
# and split to array with closing parenthesis separator
x = substr(row, index(row,p) + length(p))
split(x, a, ")")
res = p
# loop for array items and append them to substring
# until we have a string with same number of
# opening and closing parentheses.
for (i=1;i<=length(a);i++) {
res = res a[i] (i==length(a)? "": ")")
if (gsub(/\(/,"(",res) == gsub(/\)/,")",res)) {
print res
break
}
}
# will test again the rest of the row
row = substr(x, length(p))
}
}
一些基本测试
> cat file
some text length(a(b)) testing another occurence length(a))
function(length(c(d(e(f(1), 2)))) testinglength(x)
x length(y))
x length(((y))
length(length(1))
> awk -f tst.awk file
length(a(b))
length(a)
length(c(d(e(f(1), 2))))
length(x)
length(y)
length(length(1))
https://unix.stackexchange.com/questions/612528
复制相似问题