我有运行 python 的 bash 函数(它从标准输入返回所有找到的正则表达式)
function find-all() {
python -c "import re
import sys
print '\n'.join(re.findall('$1', sys.stdin.read()))"
}
当我使用这个正则表达式时find-all 'href="([^"]*)"' < index.html
它应该从正则表达式返回第一组(文件index.html中的href属性值)
我怎样才能在 sed 或 awk 中写这个?
我建议你使用grep -o
.
-o, --only-matching
Show only the part of a matching line that matches PATTERN.
E.g.:
$ cat > foo
test test test
test
bar
baz test
$ grep -o test foo
test
test
test
test
test
Update
如果您要从 html 文件中提取 href 属性,请使用如下命令:
$ grep -o -E 'href="([^"]*)"' /usr/share/vlc/http/index.html
href="style.css"
href="iehacks.css"
href="old/"
You could使用提取值cut
and sed
像这样:
$ grep -o -E 'href="([^"]*)"' /usr/share/vlc/http/index.html| cut -f2 -d'=' | sed -e 's/"//g'
style.css
iehacks.css
old/
But为了可靠性,你最好使用 html/xml 解析器。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)