강조 표시 모드를 사용한 컬러 grep 출력

강조 표시 모드를 사용한 컬러 grep 출력

다음 스크립트가 있습니다 find-file.

find . -type f -iname "*$1*" -printf '%P\n' \
| GREP_COLORS="sl=0;33;49:ms=1;34;49" grep --color=always '^\|[^/]*$' \
| grep -i --color=auto "$1"

그것이 하는 일은:

  • 경로 부분 색상화
  • 기본 이름 섹션의 패턴 색상을 지정하세요.

여기에 이미지 설명을 입력하세요.

문제: 패턴화 후 색상이 재설정됩니다.

기본 이름 부분의 경우 완전히 흰색일 수 있습니다. 이는 트릭을 수행합니다(변경할 방법을 찾지 못했지만).

경로 부분의 경우 위 스크린샷에서 볼 수 있듯이 여전히 문제가 있습니다. 색상이 재설정되어 파일 경로가 실제로 끝나는 위치를 더 이상 볼 수 없습니다!

이에 대한 해결 방법이 있나요?

편집하다 -3가지 해결 방법이 있습니다! 아래의 모든 제안은 유효하고 약간 다르지만 질문에 대한 답변입니다. 패턴의 모든 발생을 강조하는 옵션을 선택했는데 거의 연속이었지만 솔직히 거의 동일해서 선택이 어려웠습니다...

편집하다 -이를 개선하고 싶습니다. 분석 전에 조회 결과가 차단되지 않습니다. 즉, 출력이 플러시되고 한 줄씩 처리됩니다. 가능합니까?

답변1

어쩌면 이런 게 있지 않을까요?

여기에 이미지 설명을 입력하세요.

주문하다:

pattern='oRg'; find . -type f -iname "*$pattern*" -printf '%P\n' \
| GREP_COLORS="sl=0;33:mt=1;34" grep --color=always '[^/]*$' \
| GREP_COLORS="sl=1;34" grep --color=always -iP "$pattern(?=[^/]*$)" \
| GREP_COLORS="sl=0;33" grep -i "$pattern" --color

강조 표시를 원하지 않으면 마지막 줄을 제거하십시오.무늬부분 dirname.

grep의 환경 변수를 확인하십시오.GREP_COLORS섹션을 참조하세요.

답변2

zsh내장된 전역 연산자를 사용하여 이 작업을 수행할 수 있습니다. 여기에는 여러 가지 이점이 있습니다.

  • 이 문제를 쉽게 해결하세요.
  • 개행 문자가 포함된 경로 이름을 사용하세요.
  • 기본 이름에 있는 패턴만 강조 표시하기 쉽습니다.
  • 와일드카드 사용(방법에 따라 패턴을 다르게 해석 find)grep
  • 정렬된 목록을 제공합니다
  • GNU가 아닌 시스템에서도 작동합니다( -printf, -iname--color모두 비표준 확장입니다).

어쩌면 다음과 같은 것일 수도 있습니다.

#! /bin/zsh -
pattern="(#i)${1?Please specify a pattern}"

set -o extendedglob

typeset -A find_file_color
find_file_color=(
  dirname  $'\e[0;33;49m'
  basename $'\e[1;34;49m'
  match    $'\e[1;33;44m'
  reset    $'\e[m'
)

colorize_file() {
  local file=${1-$REPLY}
  case $file in
    (*/*)
      REPLY=$find_file_color[dirname]$file:h$find_file_color[reset]/;;
    (*)
      REPLY=
  esac
  REPLY+=$find_file_color[basename]${${file:t}//(#m)$~pattern/$find_file_color[match]$MATCH$find_file_color[basename]}$find_file_color[reset]
}

print -rC1 -- **/*$~pattern*(ND.+colorize_file)

print전체 목록을 작성하고 인쇄하기 전에 정렬합니다. 따라서 모든 파일을 찾은 후에야 일부 출력이 시작됩니다. 찾은 대로 인쇄하려면(그러나 정렬을 포기해야 함) glob 한정자 함수를 사용하여 컬러 파일을 인쇄할 수 있습니다.

#! /bin/zsh -
pattern="(#i)${1?Please specify a pattern}"

set -o extendedglob

typeset -A find_file_color
find_file_color=(
  dirname  $'\e[0;33;49m'
  basename $'\e[1;34;49m'
  match    $'\e[1;33;44m'
  reset    $'\e[m'
)

colorize_file() {
  local file=${1-$REPLY}
  case $file in
    (*/*)
      REPLY=$find_file_color[dirname]$file:h$find_file_color[reset]/;;
    (*)
      REPLY=
  esac
  REPLY+=$find_file_color[basename]${${file:t}//(#m)$~pattern/$find_file_color[match]$MATCH$find_file_color[basename]}$find_file_color[reset]
  print -r -- $REPLY
  false # don't bother adding the file to the glob expansion
}

: **/*$~pattern*(ND.+colorize_file)

답변3

1) 텍스트 색상 지정 방법 예시

# creation of 3 escape sequences
C_1=$(echo -ne "\033[30;47m" ) ;
C_2=$(echo -ne "\033[32;41m" ) ;
C_3=$(echo -ne "\033[37;44m" ) ;
C_N=$(echo -ne "\033[0m"  )  ;

#simple example for using it
echo $C_1 Hello in C_1 $C_N normal ;
echo $C_2 Hello in C_2 $C_N normal ;
echo $C_3 Hello in C_3 $C_N normal ;

값은 여기에 설명되어 있습니다. https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit

귀하의 경우 간단한 패턴을 찾고 있으므로 sed 명령만 사용할 수 있습니다.

PAT="tot" ; 
find ./ -iname  \*$PAT\*  -printf '%P\n' | \
    sed "s/^\(.*\/\)\{0,1\}\([^\/]*\)\($PAT\)\([^\/]*\)\$/${C_1}\1${C_2}\2${C_3}\3${C_N}\4/i"

\1 is the full path ending with the last /
\2 is the pattern of the filename before the pattern
\3 is the pattern
\4 is after the pattern

관련 정보