2012-10-05 2 views
1

그래서 Linux의 명령 줄에서 일부 HTML 코드를 검색하고 코드의 동적 부분 만 인쇄하려고합니다. 예를 들어이 코드텍스트 파일의 코드 줄에서 특정 단어 검색 및 인쇄

<p><span class="RightSideLinks">Tel: 090 97543</span></p> 

난 그냥 97543되지 090. 다음에 내가 코드가

<p><span class="RightSideLinks">Tel: 081 82827</span></p> 

로 변경했을 수 있습니다 파일을 검색하고 난 그냥 82827. 원하는을 인쇄 할 것이다 나머지 코드는 전화 번호가 변경된 그대로 동일하게 유지됩니다.

이렇게하려면 grep을 사용할 수 있습니까? 감사

편집 :

이 너무이 코드에서 사용할 수 있을까요? p1234567~S0" 나는 인쇄 할 텍스트 : 그에 어떤 변화

<tr class="patFuncEntry"><td align="left" class="patFuncMark"><input type="checkbox" name="renew0" id="renew0" value="i1061700" /></td><td align="left" class="patFuncTitle"><label for="renew0"><a href="/record=p1234567~S0"> I just want to print this part. </a></label> 

는 레코드 번호입니다. GNU grep를 사용

답변

1

한 가지 방법 : file.txt

grep -oP '(?<=Tel: .{3})[^<]+' file.txt 

예 내용 :

<p><span class="RightSideLinks">Tel: 090 97543</span></p> 
<p><span class="RightSideLinks">Tel: 081 82827</span></p> 

결과 :

97543 
82827 

편집 :

(?<=Tel: .{3}) ## This is a positive lookbehind assertion, which to be 
       ## interpreted must be used with grep's Perl regexp flag, '-P'. 

Tel: .{3}  ## So this is what we're actually checking for; the phrase 'Tel: ' 
       ## followed by any character exactly three times followed by a 
       ## space. Since we're searching only for numbers you could write 
       ## 'Tel: [0-9]{3} ' instead. 

[^<]+   ## Grep's '-o' flag enables us to return exactly what we want, 
       ## rather than the whole line. Therefore this expression will 
       ## return any character except '<' any number of times. 

Putting it all together, we're asking grep to return any character except '<' 
any number of times if we can find 'Tel: .{3} ' immediately ahead of it. HTH. 
+0

감사합니다. –

+0

grep 명령에서 어떤 일이 일어나는지 설명해 주시겠습니까? –

+0

@ManExa : 잘 설명해 봤으면 좋겠지 만, lookaround 어설 션에 대한 정보가 더 필요하면 [here] (http://www.regular-expressions.info/lookaround.html) 페이지를 참조하십시오. 어쨌든 당신이 어떻게 가는지보십시오. 문제가 있다면 그냥 물어보십시오. 또한이 답변에 만족한다면 동의하지 않으시겠습니까? 건배. – Steve

관련 문제