简体   繁体   English

如何使用sed / grep在有条件的两个单词之间提取文本?

[英]How to use sed/grep to extract text between two words with condition?

how i can extract just an online CLine (with status_icon_online)? 我如何仅提取在线CLine(带有status_icon_online)? i can extract all clines by doing : 我可以通过以下方式提取所有线索:

cat test.txt | sed -n 's:.*">C\(.*\)</a>.*:\1:p'

but i want just the online ones, the result should be like this: 但是我只想要在线的,结果应该是这样的:

C: free.cccamlux.co 21900 FREEAC5p08w cccamlux.com C:free.cccamlux.co 21900 FREEAC5p08w cccamlux.com

my test.txt file: 我的test.txt文件:

> <tr>
<td valign="middle">
<a href="/info/CCcam/free.cccamlux.co_21900_FREEAC5p08w_cccamlux.com/75e56a5655a02b839664981da2f69445937f6bbf" title="View line details for free Cline C: free.cccamlux.co 21900 FREEAC5p08w cccamlux.com">C: free.cccamlux.co 21900 FREEAC5p08w cccamlux.com</a>
</td>
<td class="text-center">
<span class="span_tooltip glyphicon glyphicon-remove-sign status_icon_online" data-toggle="tooltip" rel="tooltip" data-placement="left" title="CCcam server down or invalid cline credentials"></span>
</td>
<td align="center" valign="middle">
<a href="/info/CCcam/free.cccamlux.co_21900_FREEAC5p08w_cccamlux.com/75e56a5655a02b839664981da2f69445937f6bbf">
<button type="button" class="btn btn-primary btn-cred btn-sm btn btn-default">Show</button>
</a>
</td>
</tr>
<tr>
<td valign="middle">
<a href="/info/CCcam/palacio.iptv.re_1122_uxftgm1p_palacio/c3dbaa129dccbff15eeb7e4d6cd7d7210df38099" title="View line details for free Cline C: palacio.iptv.re 1122 uxftgm1p palacio">C: palacio.iptv.re 1122 uxftgm1p palacio</a>
</td>
<td class="text-center">
<span class="span_tooltip glyphicon glyphicon-remove-sign status_icon_offline" data-toggle="tooltip" rel="tooltip" data-placement="left" title="CCcam server down or invalid cline credentials"></span>
</td>
<td align="center" valign="middle">
<a href="/info/CCcam/palacio.iptv.re_1122_uxftgm1p_palacio/c3dbaa129dccbff15eeb7e4d6cd7d7210df38099">
<button type="button" class="btn btn-primary btn-cred btn-sm btn btn-default">Show</button>
</a>
</td>
</tr>

Maybe you can use this; 也许您可以使用它;

 awk '/<tr>/ {printf "\n%s\n",$0;next} {printf "%s ",$0}' test |  grep -o -P '(?<=Cline).*(?=online)'  |  grep -o -P '.*(?=">C)'

awk command; awk命令; merge lines between <tr> and </tr> in your file. 合并文件中<tr></tr>之间的行。

first grep ; 第一grep; get text only between Cline and online 仅在Cline和在线之间获取文本

last grep ; 最后grep; get only url parts 仅获取网址部分

Test; 测试;

$ awk '/<tr>/ {printf "\n%s\n",$0;next} {printf "%s ",$0}' test |  grep -o -P '(?<=Cline).*(?=online)'  |  grep -o -P '.*(?=">C)'
C: free.cccamlux.co 21900 FREEAC5p08w cccamlux.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM