tags:

views:

62

answers:

4
cat 1.html | grep "<title>" > title.txt  

This wont work .. please tell the best way to grab the title of a page using grep or sed ..

Thanks .

+1  A: 

You can use xml_grep from the XML::Twig Perl package:

xml_grep --text_only title 1.html
Alex Howansky
A: 
 grep "<title>" /path/to/html.html

Works fine for me. Are you sure 1.html is in your current working directory? pwd to check.

chigley
it returns <title> This is title </title> , but I want only the actual title of the page.
Krishna
+1  A: 
sed -n 's/<title>\(.*\)<\/title>/\1/Ip' 1.html

uses the combination of -n and p to only print matches

Dave
+1  A: 

you can use awk. This works even for multiline

$ cat file

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<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"&gt;

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