我想使用Linux终端上的sed命令突出显示C程序的函数名称。
我能够使用TPUT进行颜色名称上色。为此,我提供了以下代码。(第一行(
,如果我使用printf/echo/命令替换为终端的输出上色,我将无法进行着色。(代码的第二行(。我想这是因为我无法用 1和 2引用字符串。使用此功能时,它显示了其他一些字符而不是函数名称。
我使用的正则表达式读到,函数名称的第一个字符可以是字母或下划线,第二个字符可以是字母数字和下划线,第三个字符应该是开放的括号。我想通过使用 1 2和 3引用正则表达式,并为除 3以外的所有内容进行着色。这是我想到的。
我的问题是,是否有其他方法不为开放括号或使用printf和函数名称上色的方法上色。
sed -E "s,([a-zA-Z_])([a-zA-Z0-9_]*)((),$(tput setaf 1)12$(tput sgr0)3," Sample.c
sed -E "s,([a-zA-Z_])([a-zA-Z0-9_]*)((),$(printf " 33[0;36m12 33[0m3")," Sample.c
样本.c:
#include <stdio.h>
int main()
{
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in arrayn");
scanf("%d", &size);
printf("Enter %d integersn", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
return 0;
}
预期结果 -> main,printf,scanf应在样本中涂上颜色。
tput很聪明,但是嵌入式printf无法解析,因为它在子壳中,因此printf无法正常工作。
在var=$'ansi-ized content'
语法中可能有一个狂欢。三个捕获组似乎没有NECC。如此省略:
BEGC=$' 33[0;36m' ENDC=$' 33[0m';
sed -E "s,([a-zA-Z_][a-zA-Z0-9_]*)((),${BEGC}1${ENDC}2," Sample.c
但是,还有另一个更基本的问题,因为嵌套功能不会突出显示。在更新的示例中注意,在这里,虚拟的" getize(("功能不会突出显示:
#include <stdio.h>
int main()
{
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in arrayn");
scanf("%d", &size);
printf("Enter %d integersn", getSize(size));
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
return 0;
}
一个简单的正则是有递归要求,因此不起作用。由于它具有段循环和函数(gensub((,也许?(