使用grep提取html文件的标题

Vam*_*a B 3 bash shell grep

cat 1.html | grep "<title>" > title.txt  
Run Code Online (Sandbox Code Playgroud)

这个grep语句不起作用.

请告诉使用grep或sed获取页面标题的最佳方法.

谢谢.

小智 5

sed -n 's/<title>\(.*\)<\/title>/\1/Ip' 1.html
Run Code Online (Sandbox Code Playgroud)

使用-n和p的组合仅打印匹配


gho*_*g74 5

你可以使用awk.这甚至适用于多线

$ cat file

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

    <title>Extract Title of a html file

using grep - Stack Overflow</title>
    <link rel="stylesheet" type="text/css" href="http://sstatic.net/stackoverflow/all.css?v=9ea1a272f146">

$ awk -vRS="</title>" '/<title>/{gsub(/.*<title>|\n+/,"");print;exit}' file
Extract Title of a html file using grep - Stack Overflow
Run Code Online (Sandbox Code Playgroud)