2

我一直在寻找很长时间,但无法找到解决我问题的有效答案。

我从用 提取的 HTML 文件中有一行sed '162!d' skinlist.html,其中包含文本

<a href="/skin/dwarf-red-beard-734/" title="Dwarf Red Beard">.

我想提取文本Dwarf Red Beard,但该文本是模块化的(可以更改),所以我想提取和之间的title="文本"

对于我的一生,我无法弄清楚如何做到这一点。

4

5 回答 5

2
awk 'NR==162 {print $4}' FS='"' skinlist.html
  • 将字段分隔符设置为"
  • 仅打印第 162 行
  • 打印字段 4
于 2013-05-23T05:11:37.640 回答
1

sed 中的解决方案

sed -n '162 s/^.*title="\(.*\)".*$/\1/p' skinlist.html

提取 line in162skinlist.html捕获.title\1

于 2013-05-23T05:32:10.053 回答
0

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
于 2013-05-23T05:10:28.217 回答
0

您可以通过另一个传递它sed或向它添加表达式sed-e 's/.*title="//g' -e 's/">.*$//g'

于 2013-05-23T05:21:16.867 回答
0

还 sed

sed -n '162 s/.*"\([a-zA-Z ]*\)"./\1/p' skinlist.html
于 2013-05-23T10:11:24.393 回答