Kolorowy grep?

Czasami kolorowanie pliku dziennika lub innego daje dobry przegląd podczas wyszukiwania rzeczy i zachowań

Właśnie widziałem, że grep ma funkcję kolorowania

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

Jakie są inne metody?

Author: epatel, 2008-09-26

5 answers

Do wyszukiwania kodu źródłowego używam ack . Ma wiele opcji, które mają sens do wyszukiwania kodu (takich jak automatyczne ignorowanie katalogów SCM).

 14
Author: Greg Hewgill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-26 09:06:52

Oto fragment narzędzia do kolorowania dziennika, którego czasami używam.

Zauważ, że działa tylko przeciwko standardom stdin/stdout oraz w terminalu obsługującym kolory ANSI.

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

#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 ] <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;
}
 20
Author: epatel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-02 20:20:38

Istnieje wiele programów obsługujących kolorowanie, takich jak Colortail

Może to też może być pomocne: GenericColouriser

 3
Author: unexist,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-26 09:08:33

Używamy baretail , Teraz, jeśli dodali kolor do swojego baregrep, byłoby miło.

 1
Author: Chris Kimpton,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-26 09:35:57

To jest starsze pytanie, ale na wypadek, gdyby ktoś jeszcze szukał, niedawno stworzyłem colorize, narzędzie, które pozwala określić stałe wzorce lub wyrażenia regularne, aby dopasować je do określonych kolorów. Działa po wyjęciu z pudełka z intuicyjną składnią do określania podświetlania i docopt jako jedyna zależność.

colorize.py -f 'This is an interesting line=Blue' -f 'Different topic=Red' Input.log
 1
Author: merlin2011,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-08 08:24:49