当前位置:  开发笔记 > 编程语言 > 正文

有色grep?

如何解决《有色grep?》经验,为你挑选了2个好方法。

有时,在查找内容行为时,着色日志文件或其他内容可以提供良好的概述

我刚看到grep有着色功能

grep -C 99999 --color  

还有哪些其他方法?



1> epatel..:

这是我有时使用的日志着色工具的片段.

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

#include 
#include 

#define MAX_LINE 4096

#define RESET   "\033[0m"
#define BLACK   "\033[30m"      /* Black */
#define RED     "\033[31m"      /* Red */
#define GREEN   "\033[32m"      /* Green */
#define YELLOW  "\033[33m"      /* Yellow */
#define BLUE    "\033[34m"      /* Blue */
#define MAGENTA "\033[35m"      /* Magenta */
#define CYAN    "\033[36m"      /* Cyan */
#define WHITE   "\033[37m"      /* White */
#define BOLDBLACK   "\033[1m\033[30m"      /* Bold Black */
#define BOLDRED     "\033[1m\033[31m"      /* Bold Red */
#define BOLDGREEN   "\033[1m\033[32m"      /* Bold Green */
#define BOLDYELLOW  "\033[1m\033[33m"      /* Bold Yellow */
#define BOLDBLUE    "\033[1m\033[34m"      /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m"      /* Bold Magenta */
#define BOLDCYAN    "\033[1m\033[36m"      /* Bold Cyan */
#define BOLDWHITE   "\033[1m\033[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 ] \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;
}



2> Greg Hewgill..:

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

推荐阅读
勤奋的瞌睡猪_715
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有