Bash 在不同行上的多个模式匹配之后,如何替换第n行上的单词?
我试图找出是否有一种方法可以在特定条件下使用SED或AWK替换单词。示例代码:Bash 在不同行上的多个模式匹配之后,如何替换第n行上的单词?,bash,awk,sed,Bash,Awk,Sed,我试图找出是否有一种方法可以在特定条件下使用SED或AWK替换单词。示例代码: void className::functName1\u testName1\u paramA() { 一些代码; /*评论*/ returnType_1 paramA=value1; returnType_2 paramB=value2; 返回类型_3参数c=值3; } void className::functName1_testName2_paramB() { 一些代码; /*评论*/ returnType_1
void className::functName1\u testName1\u paramA()
{
一些代码;
/*评论*/
returnType_1 paramA=value1;
returnType_2 paramB=value2;
返回类型_3参数c=值3;
}
void className::functName1_testName2_paramB()
{
一些代码;
/*评论*/
returnType_1 paramA=value1;
returnType_2 paramB=value2;
返回类型_3参数c=值3;
}
void className::functName2_testName1_paramA()
{
一些代码;
/*评论*/
returnType_1 paramA=value1;
returnType_2 paramB=value2;
返回类型_3参数c=值3;
returnType_4参数=value4;
}
我想要达到的是
IF (functName1 && testName1 && paramA are found/matched) THEN
IF (returnType_1 && paramA are found/matched in nth line below above match) THEN
replace value1 with actualValue1;
IF (functName1 && testName2 && paramB are found/matched) THEN
IF (returnType_2 && paramB are found/matched in nth line below above match) THEN
replace value2 with actualValue2;
IF (functName2 && testName1 && paramA are found/matched) THEN
IF (returnType_1 && paramA are found/matched in nth line below above match) THEN
replace value1 with actualValue1;
诸如此类
我有很多不同函数名、测试名、参数和返回类型组合的模板,所以我希望能够像上面那样匹配它们
我考虑过使用
sed '/functName1/ { n; n; n; s/value1/actualValue1/; }' fileName
但它不起作用,因为有太多不同的情况,不同的函数的第n行不同。所以,当我在寻找答案时,人们似乎在说,对于这样的东西,awk
比sed
更好,但我真的找不到与我的情况有点类似的东西
我假设有办法做到这一点,但我不太确定。所以我想问题是,像这样的事情在awk
或sed
中也可能发生吗
我不介意多次编写awk
/sed
命令,因此,如果有人能给我一个示例以及这个awk
/sed
命令用法的语法,我可以应用这个概念并编写其余的
我真的很感谢您抽出时间,并提前向您表示感谢
预期的产出将是
void className::functName1_testName1_paramA()
{
some_code;
/* comment */
returnType_1 paramA = actualValue1; //after being replaced
returnType_2 paramB = value2;
returnType_3 paramC = value3;
}
void className::functName1_testName2_paramB()
{
some_code;
/* comment */
returnType_1 paramA = value1;
returnType_2 paramB = actualValue2; //after being replaced
returnType_3 paramC = value3;
}
void className::functName2_testName1_paramA()
{
some_code;
/* comment */
returnType_1 paramA = actualValue1; //after being replaced
returnType_2 paramB = value2;
returnType_3 paramC = value3;
returnType_4 paramD = value4;
}
请您尝试以下内容,完全基于展示的样品
awk '
!NF{
found_func2_test1_parama=found_func2_test1_paramb=found_func1_test1_parama=""
}
/functName1_testName1_paramA/{
found_func1_test1_parama=1
}
/returnType_1 paramA/ && found_func1_test1_parama{
$NF="actualValue1;"
sub(/returnType/," &")
print
next
}
/functName2_testName1_paramB/{
found_func2_test1_paramb=1
}
found_func2_test1_paramb && /returnType_2 paramB/{
$NF="actualValue2;"
sub(/returnType/," &")
print
next
}
/functName2_testName1_paramA/{
found_func2_test1_parama=1
}
found_func2_test1_parama && /returnType_1 paramA/{
$NF="actualValue1;"
sub(/returnType/," &")
print
next
}
1
' Input_file
预期输出是什么?抱歉,刚刚在帖子上添加了预期输出。谢谢。
awk
是为解决这些问题而设计的。通过模式部分,你将准备好在这里问一个好问题。祝你好运。请注意,当你没有使用这些特定工具的真正的、潜在的技术原因时,指定“使用A或B执行X”是一种不好的形式,只是碰巧期望这是人们通常会使用的。甚至“使用POSIX标准化UNIX工具执行X”也更好,因为它可以让回答者(提供领域知识)确定哪些特定的POSIX标准化UNIX工具最适合执行任务。@RavinderSingh13:我很抱歉。我把变量弄混了。打得好,因为这和我想解释的不一致。谢谢你的接球。