find 및 grep 후에 파일 행을 출력하는 방법은 무엇입니까?

find 및 grep 후에 파일 행을 출력하는 방법은 무엇입니까?

안녕하세요. 현재 날짜에 대한 로그 파일의 오류 메시지를 출력하고 싶습니다.

먼저 특정 접두사가 붙은 오늘의 로그를 검색합니다.

find /home/USER/logfilesError/ -maxdepth 1 -type f -name "xy_*" -daystart -mtime -1

그것은 나에게 출력을 제공합니다 :

/home/USER/logfilesError/xy_2071.log
/home/USER/logfilesError/xy_2072.log
/home/USER/logfilesError/xy_2073.log

이 파일에서 "ERROR" 문자열을 검색하고 싶습니다.

grep -rl "ERROR" /home/USER/logfilesError/

그러면 오늘뿐만 아니라 "오류"가 포함된 모든 로그 파일이 제공됩니다.

출력(부분 출력만):

/home/USER/logfilesError/xy_55.log
/home/USER/logfilesError/xy_1015.log

질문:

이를 스크립트로 결합하려면 어떻게 해야 합니까?

로그 파일의 한 줄 구문은 다음과 같습니다.

2013-11-24 06:30:30,549 [main] ERROR *(+Errormessage)*

답변1

이 시도:

find /home/USER/logfilesError/ -maxdepth 1 -type f -name "xy_*" \
    -daystart -mtime -1 -exec grep -Hl "ERROR" "{}" +

에서 man find:

-exec command {} +
              This  variant  of the -exec action runs the specified command on
              the selected files, but the command line is built  by  appending
              each  selected file name at the end; the total number of invoca‐
              tions of the command will  be  much  less  than  the  number  of
              matched  files.   The command line is built in much the same way
              that xargs builds its command lines.  Only one instance of  `{}'
              is  allowed  within the command.  The command is executed in the
              starting directory.

관련 정보