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

时间:2016-12-01 23:49:28

标签: regex shell awk sed extract

我如何只提取一个在线CLine(带有status_icon_online)?我可以通过以下方式提取所有clines:

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

但我只想在线,结果应该是这样的:

  

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

我的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>

1 个答案:

答案 0 :(得分:0)

也许你可以使用它;

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

awk命令;合并文件中<tr></tr>之间的行。

先是grep;只在Cline和在线之间获取文本

最后一次grep;只获取网址

测试;

$ 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