使用 linux command/srcipting 在 HTML 的特定单词后提取单词
Extract word after a specific word of HTML using linux command/srcipting
我有一个文件 '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并提供一个header'title',例子:
title
1 Buck (2017)/
1 Mile to You (2017)/
1 Night (2016)/
如何使用 linux 命令(如 awk、sed 或其他命令)获得与上述文件类似的输出文件。
我试过这段代码:
awk '{for (I=1;I<NF;I++) if ($I == "%29/">") print $(I+1)}' file
使用您展示的示例,请尝试以下操作。
awk 'BEGIN{print "title"} match([=10=],/%29\/">[^/]*/){print substr([=10=],RSTART+6,RLENGTH-5)}' Input_file
说明:为以上代码添加详细说明。
awk ' ##Starting awk program from here.
BEGIN{print "title"}
match([=11=],/%29\/">[^/]*/){ ##Using match function to match regex %29\/"> till / here.
print substr([=11=],RSTART+6,RLENGTH-5) ##Printing sub string here.
}
' Input_file ##Mentioning Input_file name here.
还可以使用 awk
将 FS
设置为 '[><]'
并打印 </code>:</p>
<pre><code>awk -F'[><]' 'BEGIN{ print "title" } /%29/ {print }' file
title
1 Buck (2017)/
1 Mile to You (2017)/
1 Night (2016)/
或者这个使用 </code> 的最后一个(你需要的条件):</p>
<pre><code>awk -F'[><]' 'BEGIN{ print "title" } ~ /%29\/"$/ {print }' file
title
1 Buck (2017)/
1 Mile to You (2017)/
1 Night (2016)/
我有一个文件 '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并提供一个header'title',例子:
title
1 Buck (2017)/
1 Mile to You (2017)/
1 Night (2016)/
如何使用 linux 命令(如 awk、sed 或其他命令)获得与上述文件类似的输出文件。
我试过这段代码:
awk '{for (I=1;I<NF;I++) if ($I == "%29/">") print $(I+1)}' file
使用您展示的示例,请尝试以下操作。
awk 'BEGIN{print "title"} match([=10=],/%29\/">[^/]*/){print substr([=10=],RSTART+6,RLENGTH-5)}' Input_file
说明:为以上代码添加详细说明。
awk ' ##Starting awk program from here.
BEGIN{print "title"}
match([=11=],/%29\/">[^/]*/){ ##Using match function to match regex %29\/"> till / here.
print substr([=11=],RSTART+6,RLENGTH-5) ##Printing sub string here.
}
' Input_file ##Mentioning Input_file name here.
还可以使用 awk
将 FS
设置为 '[><]'
并打印 </code>:</p>
<pre><code>awk -F'[><]' 'BEGIN{ print "title" } /%29/ {print }' file
title
1 Buck (2017)/
1 Mile to You (2017)/
1 Night (2016)/
或者这个使用 </code> 的最后一个(你需要的条件):</p>
<pre><code>awk -F'[><]' 'BEGIN{ print "title" } ~ /%29\/"$/ {print }' file
title
1 Buck (2017)/
1 Mile to You (2017)/
1 Night (2016)/