Linux的shell,sed或awk之类工具替换函数?

来源:百度知道 编辑:UC知道 时间:2024/05/31 09:18:41
某个文件(test.c)比如包含如下函数(但不仅有这个函数,还有main、全局变量、注释等):
int my_function (int i)
{
if(i>0){
printf("TRUE\n");
retrun 0;
}else{
printf("FALSE\n");
retrun 1;
}
}

shell里(不是用vi编辑,因为要写在makefile里)用命令,把这个文件中的my_function整个函数复制一遍,并改名为my_function_bk。写在原函数下面。

命令如何打?或提供一个可用的shell脚本。解决后一定加分!!!
PS. 用bash。文件(test.c)中函数与函数之间不保证有什么(比如有注释、全局变量的定义等...) 谢谢!!! 急! 等!
谢谢关注~
清明节没去扫墓宅在家写出来了个.... 太长 竟然贴不上来... -_-|||

关注

可以给我发送邮件让我看看嘛 谢谢
jeloov@yahoo.com.cn

自虐啊
要是函数内容固定的,可以试试patch
否则的话,得有一个专业的分析c语法的东西吧。。

can't really understand

something like this?

nc10@your-5554c55be4 ~
$ cat bb
#! /bin/bash

while read -r;
do
if [[ "$REPLY" =~ (int my_function)( .*) ]]
then
REPLY=${BASH_REMATCH[1]}"_bk"${BASH_REMATCH[2]}
else
REPLY=$REPLY
fi
printf "%s\n" "$REPLY"
done < test.c >> urfile

nc10@your-5554c55be4 ~
$ rm urfile
rm:是否删除 普通文件 “urfile”? y

nc10@your-5554c55be4 ~
$ sh bb

nc10@your-5554c55be4 ~
$ cat urfile
int my_function_bk (int i)
{
if(i>0){
printf("TRUE\n");
retrun 0;
}else{
printf("FALSE\n");
retrun 1;
}
}

nc10@your-5554c55be4 ~
$