微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 linux 命令/srcipting 在 HTML 的特定单词之后提取单词

如何解决使用 linux 命令/srcipting 在 HTML 的特定单词之后提取单词

我有一个文件“tes.html”:

<html>
<head><title>Index of /Data/Movies/Hollywood/2016_2017/</title></head>
<body bgcolor="white">
<h1>Index of /Data/Movies/Hollywood/2016_2017/</h1><hr><pre><a href="../">../</a>
<a href="1%20Buck%20%282017%29/">1 Buck (2017)/</a>                                     25-Nov-2019 10:25       -
<a href="1%20Mile%20to%20You%20%282017%29/">1 Mile to You (2017)/</a>                              25-Nov-2019 10:26       -
<a href="1%20Night%20%282016%29/">1 Night (2016)/</a>                                    25-Nov-2019 10:27       -
</pre><hr></body>
</html>

我想将 "% 29 / ">" 之后的值获取到 output.txt 并提供标题“title”,例如:

title
1 Buck (2017)/
1 Mile to You (2017)/
1 Night (2016)/

如何使用 awk、sed 等 linux 命令获取上述输出文件

我试过这个代码

awk '{for (I=1;I<NF;I++) if ($I == "%29/">") print $(I+1)}' file

解决方法

对于您展示的样本,请尝试以下操作。

awk 'BEGIN{print "title"} match($0,/%29\/">[^/]*/){print substr($0,RSTART+6,RLENGTH-5)}'  Input_file

说明:为以上代码添加详细说明。

awk '                                   ##Starting awk program from here.
BEGIN{print "title"}
match($0,/%29\/">[^/]*/){               ##Using match function to match regex %29\/"> till / here.
  print substr($0,RLENGTH-5)   ##Printing sub string here.
}
'  Input_file                           ##Mentioning Input_file name here.
,

还可以使用 awkFS 设置为 '[><]' 并打印 $3

awk -F'[><]' 'BEGIN{ print "title" } /%29/ {print $3}' file
title
1 Buck (2017)/
1 Mile to You (2017)/
1 Night (2016)/

或使用 $2 的最后一个(您需要的条件):

awk -F'[><]' 'BEGIN{ print "title" } $2 ~ /%29\/"$/ {print $3}' file
title
1 Buck (2017)/
1 Mile to You (2017)/
1 Night (2016)/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。