如何删除锚标记,但将锚文本保留在Bash中?所以我想删除除示例文本之外的所有内容.
<a href="http://example.com">Example text</a>
Run Code Online (Sandbox Code Playgroud)
所以,如果我这样做:
echo '<a href="http://example.com">Example text</a>' | sed -e 's/<[^>]*>//g'
Run Code Online (Sandbox Code Playgroud)
这删除了所有的HTML.我想删除锚标签,但也保留锚文本...也就是这种情况下的示例文本.
您可以使用以下命令:
$ echo '<a href="http://example.com">Example text</a>' | sed -e 's/<[^>]*>//g'
Example text
Run Code Online (Sandbox Code Playgroud)
或者,也可以使用,perl而不是sed因为非贪婪的正则表达式在这里有用:
$ echo '<a href="http://example.com">Example text</a>' | perl -pe 's/\<.*?\>//g'
Example text
Run Code Online (Sandbox Code Playgroud)
注意:不鼓励使用正则表达式来解析HTML ,但对于这个小任务,我会说坚持使用命令行中的可用工具.
编辑:要删除只是锚标签,可以使用正则表达式,可以更新如下:
sed -e 's/<\/\?a\s*[^>]*>//g'
Run Code Online (Sandbox Code Playgroud)