我一直在寻找很长时间,但无法找到解决我问题的有效答案。
我从用 提取的 HTML 文件中有一行sed '162!d' skinlist.html
,其中包含文本
<a href="/skin/dwarf-red-beard-734/" title="Dwarf Red Beard">
.
我想提取文本Dwarf Red Beard
,但该文本是模块化的(可以更改),所以我想提取和之间的title="
文本"
。
对于我的一生,我无法弄清楚如何做到这一点。
awk 'NR==162 {print $4}' FS='"' skinlist.html
"
sed 中的解决方案
sed -n '162 s/^.*title="\(.*\)".*$/\1/p' skinlist.html
提取 line in162
并skinlist.html
捕获.title
\1
shell 的变量扩展语法允许您从字符串中修剪前缀和后缀:
line="$(sed '162!d' skinlist.html)" # extract the relevant line from the file
temp="${line#* title=\"}" # remove from the beginning through the first match of ' title="'
if [ "$temp" = "$line" ]; then
echo "title not found in '$line'" >&2
else
title="${temp%%\"*}" # remote from the first '"' through the end
fi
您可以通过另一个传递它sed
或向它添加表达式sed
-e 's/.*title="//g' -e 's/">.*$//g'
还 sed
sed -n '162 s/.*"\([a-zA-Z ]*\)"./\1/p' skinlist.html