【发布时间】:2019-12-13 14:40:21
【问题描述】:
我想在 linux 终端上使用 sed 命令突出显示 C 程序的函数名称。
我可以使用 tput 为函数名称着色。我为此提供了下面的代码。 (第一行)
如果我使用 printf/echo/command 替换为终端的输出着色,我将无法着色。 (第二行代码)。我想这是因为我无法使用 \1 和 \2 引用字符串。使用它时,它会显示一些其他字符而不是函数名称。
我使用的正则表达式显示,函数名的第一个字符可以是字母或下划线,第二个字符可以是字母数字和下划线,第三个字符应该是一个左括号。我想通过使用 \1 \2 和 \3 来引用正则表达式,并对除 \3 之外的所有内容进行着色。这是我想出的主意。
我的问题是,有没有其他方法可以不为左括号着色或使用 printf 并为函数名称着色。
sed -E "s,([a-zA-Z_])([a-zA-Z0-9_]*)(\(),$(tput setaf 1)\1\2$(tput sgr0)\3," Sample.c
sed -E "s,([a-zA-Z_])([a-zA-Z0-9_]*)(\(),$(printf "\033[0;36m\1\2\033[0m\3")," Sample.c
示例 .c:
#include <stdio.h>
int main()
{
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
return 0;
}
预期结果 -> main、printf、scanf 应该在 Sample.c 中着色。
【问题讨论】:
标签: regex linux shell sed terminal