linux - 使用 linux command/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,RSTART+6,RLENGTH-5) ##Printing sub string here.
}
' Input_file ##Mentioning Input_file name here.
推荐阅读
- python - 使用 pd.ExcelFile().parse() 从读取中排除列
- html - 如何覆盖表的默认css
- javascript - 使用 Wikipedia 的 opensearch api 时,主题标签符号不会填充数据列表。为什么?
- r - 即使在升级服务器实例后也无法分配大小为 xxx 的向量
- javascript - 登录后如何在php中为欢迎消息制作过渡效果?
- go - 如何不舍入持续时间
- java - 在具体类中,我的 ObjectOutputStream 不起作用,没有显示正确的结果
- postgresql - Postgresql 外来数据包装器
- jquery - 获取jQuery UI中拖放元素的自定义HTML属性
- python - Python:在文本文件行中分配一定数量的符号