access.log 줄을 시간별로 복사하는 방법은 무엇입니까?

access.log 줄을 시간별로 복사하는 방법은 무엇입니까?

CentOS에 거대한 웹 서버 access.log가 있습니다. 원격 VPN을 통해 액세스하므로 파일을 복사하거나 직접 읽을 수 없습니다.

복사하려는 로그의 특정 시간을 알고 있지만 너무 오래 전이므로 로그의 꼬리를 텍스트 파일로 쉽게 복사할 수 있습니다. 로그의 한 줄은 다음과 같습니다.

10.255.16.203 - - [26/Mar/2014:16:35:13 +0000]

그래서 내 질문은:찾고자 하는 시간 문자열을 알고 있는 경우 매우 큰 로그의 특정 섹션을 어떻게 복사할 수 있습니까?

답변1

grep명령은 주어진 파일과 일치하는 행만 표시하도록 설계되었습니다. 옵션을 사용하면 -C일치하는 선뿐만 아니라 그 전후의 일부 선도 표시할 수 있습니다.

따라서 원하는 줄 앞뒤에 3줄을 추가하려면 다음을 수행하세요.

$ grep -C 3 "26/Mar/2014:16:35:13 +0000" access.log

-A또한 및 옵션 을 사용하여 일치하는 줄 뒤 및/또는 앞에 표시되는 줄 수를 더 정확하게 조정할 수도 있습니다 -B. 실제로 -C 3는 와 동일합니다 -A 3 -B 3.

일치하는 줄이 두 개 이상 있으면 grep일치하는 줄 블록 앞뒤에 3개의 줄이 표시됩니다.

예:

$ grep -C 3 "25/Mar/2014:10:40:59 +0100" access.log
10.0.0.44 - httpuse [25/Mar/2014:09:41:17 +0100] "GET /dummy/BIGDummy_133644_1565_DL.xml.gz HTTP/1.1" 200 507 "-" "-"
10.0.0.43 - httpuse [25/Mar/2014:09:59:51 +0100] "GET /dummy/BIGDummy_133647_48267_DL.xml.gz HTTP/1.1" 200 1677 "-" "-"
10.0.0.44 - httpuse [25/Mar/2014:10:40:42 +0100] "GET /dummy/BIGDummy_133664_39603_DL.xml.gz HTTP/1.1" 200 1677 "-" "-"
10.0.0.40 - httpuse [25/Mar/2014:10:40:59 +0100] "GET /dummy/BIGDummy_133664_DL.xml.gz HTTP/1.1" 200 60142 "-" "-"
10.0.0.41 - httpuse [25/Mar/2014:10:40:59 +0100] "GET /dummy/BIGDummy_133667_23124_DL.xml.gz HTTP/1.1" 200 5202 "-" "-"
10.0.0.40 - httpuse [25/Mar/2014:10:43:09 +0100] "GET /dummy/BIGDummy_133668_46_DL.xml.gz HTTP/1.1" 200 445 "-" "-"
10.0.0.42 - httpuse [25/Mar/2014:10:43:10 +0100] "GET /dummy/BIGDummy_133668_4116_DL.xml.gz HTTP/1.1" 200 597 "-" "-"
10.0.0.40 - httpuse [25/Mar/2014:10:43:13 +0100] "GET /dummy/BIGDummy_133665_DL.xml.gz HTTP/1.1" 200 57902 "-" "-"

에서 man grep:

NAME
   grep, egrep, fgrep - print lines matching a pattern

SYNOPSIS
   grep [options] PATTERN [FILE...]

DESCRIPTION
   Grep  searches  the  named  input  FILEs (or standard input if no files are named,
    or the file name - is given) for lines containing a match to the given PATTERN.
   By default, grep prints the matching lines.

OPTIONS
    -A NUM, --after-context=NUM
            Print  NUM  lines  of  trailing context after matching lines.
            Places a line containing -- between contiguous groups of matches.

    -B NUM, --before-context=NUM
            Print NUM lines of leading context before matching lines.
            Places a line containing --  between  contiguous  groups  of matches.

    -C NUM, --context=NUM
            Print  NUM  lines  of  output  context.
            Places a line containing -- between contiguous groups of matches.

관련 정보