有色grep?

时间:2020-03-06 14:46:23  来源:igfitidea点击:

在查找内容和行为时,有时为日志文件或者其他文件着色可以很好地概述

我刚刚看到grep具有着色功能

grep -C 99999 --color <regexp> <filename>

还有什么其他方法?

解决方案

为了搜索源代码,我使用ack。它有许多对于搜索代码有意义的选项(例如自动忽略SCM目录)。

有很多支持着色的程序,例如Colortail

也许这也可能会有所帮助:GenericColouriser

我们使用无尾辫,现在,如果他们在Baregrep中添加颜色,那就太好了。

这是我有时使用的原木着色工具的片段。

请注意,这仅适用于stdin / stdout以及支持ANSI颜色的终端。

#include <stdio.h>
#include <regex.h>

#define MAX_LINE 4096

#define RESET   "3[0m"
#define BLACK   "3[30m"      /* Black */
#define RED     "3[31m"      /* Red */
#define GREEN   "3[32m"      /* Green */
#define YELLOW  "3[33m"      /* Yellow */
#define BLUE    "3[34m"      /* Blue */
#define MAGENTA "3[35m"      /* Magenta */
#define CYAN    "3[36m"      /* Cyan */
#define WHITE   "3[37m"      /* White */
#define BOLDBLACK   "3[1m3[30m"      /* Bold Black */
#define BOLDRED     "3[1m3[31m"      /* Bold Red */
#define BOLDGREEN   "3[1m3[32m"      /* Bold Green */
#define BOLDYELLOW  "3[1m3[33m"      /* Bold Yellow */
#define BOLDBLUE    "3[1m3[34m"      /* Bold Blue */
#define BOLDMAGENTA "3[1m3[35m"      /* Bold Magenta */
#define BOLDCYAN    "3[1m3[36m"      /* Bold Cyan */
#define BOLDWHITE   "3[1m3[37m"      /* Bold White */

static int selected_color = 0;
static char *colors[] = {
  "-green", GREEN,
  "-black", BLACK,
  "-red", RED,
  "-yellow", YELLOW,
  "-blue", BLUE,
  "-magenta", MAGENTA,
  "-cyan", CYAN,
  "-white", WHITE,
  "-boldgreen", BOLDGREEN,
  "-boldblack", BOLDBLACK,
  "-boldred", BOLDRED,
  "-boldyellow", BOLDYELLOW,
  "-boldblue", BOLDBLUE,
  "-boldmagenta", BOLDMAGENTA,
  "-boldcyan", BOLDCYAN,
  "-boldwhite", BOLDWHITE,
  NULL
};

/*----------------------------------------------------------------------*/

int main(int argc, char *argv[]) {
  char buf[MAX_LINE];
  int has_re = 0;
  regex_t re;

  if (argc > 1) {
    if (argc > 2) {
      int idx = 0;
      while (colors[idx*2]) {
        if (!strcmp(colors[idx*2], argv[1])) {
          selected_color = idx;
          break;
        }
        idx++;
      }
      if (regcomp(&re, argv[2], REG_EXTENDED | REG_NEWLINE)) {
        printf("regcomp() failed!\n");
        return -1;
      }
    } else if (regcomp(&re, argv[1], REG_EXTENDED | REG_NEWLINE)) {
      printf("regcomp() failed!\n");
      return -1;
    }
    has_re = 1;
  } else {
    printf("Usage: %s [ -red | -blue | -cyan | -white | -black | "
           "-yellow | -magenta ] <regexp>\n", argv[0]);
    return -1;  
  }

  while (fgets(buf, MAX_LINE, stdin) == buf) {
    char *bbuf = buf;
    while (1) {
      if (has_re) {
        regmatch_t match[10];
        if (regexec(&re, bbuf, re.re_nsub + 1, match, 0)) {
          printf("%s", bbuf);
          break;
        } else {
          int i, idx;
          for (i=idx=0; i<1; i++) {
            if (match[0].rm_so < 0) {
              break;
            } else {
              printf("%.*s", 
                     (int)(match[i].rm_so-idx), 
                     bbuf+idx);
              printf( "%s%.*s" RESET, 
                      colors[selected_color*2+1],
                      (int)(match[i].rm_eo-match[i].rm_so), 
                      bbuf+(int)match[i].rm_so);
              idx = match[i].rm_eo;
              bbuf += idx;
            }
          }
        }
      }
      fflush(stdout);
    }
  }

  if (has_re) {
    regfree(&re);
  }

  return 0;
}