2012-03-14 2 views
1

가능한 모든 문제를 나열하는 텍스트 파일이 있습니다. 항상 URL로 시작한 다음 Result와 오류 코드로 끝납니다. 내가하고 싶은 일은 txt 파일을 살펴보고 모든 Error : 404 Not Found 블록을 가져 와서 모든 텍스트를 별도의 텍스트 파일로 출력하는 것입니다. 200 OK ... 내가 것 정말 좋아 :정규식의 실제 반전

awk '/URL/,/404 Not Found/' text.txt > only404.txt

문제는 그것이 경우에 아래도 유효을 포함 할 것이다 찾을 수 없음 (404)에 도달 할 때까지 찾고 중지 한 후 URL을 발견하고있다 :이 발견 do는 404 Not Found를 검색 한 다음 URL에 도달 할 때까지 역순입니다. 그러면 효과가 있습니다. 어떤 아이디어?

URL //fonts.googleapis.com/css?family=Lato:300,400,400italic,700' 
    Parent URL http://example.com, line 12, col 1 
    Real URL http://fonts.googleapis.com/css?family=Lato:300,400,400italic,700 
    Check time 1.863 seconds 
    Warning Access denied by robots.txt, skipping content checks. 
    Result  Valid: 200 OK 

    URL `/image.png' 
    Parent URL http://example.com/styles.css, line 1380, col 17 
    Real URL http://example.com/image.png 
    Check time 0.443 seconds 
    Size  1KB 
    Result  Error: 404 Not Found 

답변

3

이 당신을 위해 작동 할 수 있습니다

awk -v RS="" '/404 Not Found/' yourFile 

시험이 당신이 원하는 무엇인가?

kent$ cat t 
    URL //fonts.googleapis.com/css?family=Lato:300,400,400italic,700' 
    Parent URL http://example.com, line 12, col 1 
    Real URL http://fonts.googleapis.com/css?family=Lato:300,400,400italic,700 
    Check time 1.863 seconds 
    Warning Access denied by robots.txt, skipping content checks. 
    Result  Valid: 200 OK 

    URL `/image.png' 
    Parent URL http://example.com/styles.css, line 1380, col 17 
    Real URL http://example.com/image.png 
    Check time 0.443 seconds 
    Size  1KB 
    Result  Error: 404 Not Found 

kent$ awk -v RS="" '/404 Not Found/' t 
    URL `/image.png' 
    Parent URL http://example.com/styles.css, line 1380, col 17 
    Real URL http://example.com/image.png 
    Check time 0.443 seconds 
    Size  1KB 
    Result  Error: 404 Not Found 
+0

아니오. – user983223

+0

@ user983223 테스트를 추가했는데 원하는 결과가 출력 되었습니까? – Kent

+0

예. 고맙습니다. 왜 그것이 효과가 있었는지 모르겠습니다. 왜 그랬는지 말해 줄 수 있어요? 나는 awk 남자를보고 있고, 따라 가지 않고있다. – user983223

1

이 당신을 위해 작동 될 수 있습니다 작동하지 않았다

sed '/^\s*URL/,/^\s*Result/{/^\s*URL/{h;d};H;/Error: 404/{g;b}};d' file 
    URL `/image.png' 
    Parent URL http://example.com/styles.css, line 1380, col 17 
    Real URL http://example.com/image.png 
    Check time 0.443 seconds 
    Size  1KB 
    Result  Error: 404 Not Found