tags:

views:

34

answers:

2

Hey,

I am trying to extract the page title from an HTML page

cat index.html | grep -i "title>"| sed 's/<title>/ /i'| sed 's/<\/title>/ /i'

The problem happens when some pages are written in one line! (believe me it happens)

How do I solve that?

Thanks!

+1  A: 
sed -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q'

From Linux Commands.

1st result for Google: unix extract page title.

mcandre
Thank you so much!
Samantha
A: 

this awk one liner works also for title that spans more than 1 line.

$ cat file
<html>
    <title>How to extract a page
title - Stack Overflow</title>
    <link rel="stylesheet" href="http://sstatic.net/so/all.css?v=4864b39b46cf"&gt;
    <link rel="shortcut icon" href="http://sstatic.net/so/favicon.ico"&gt;
    <link rel="apple-touch-icon" href="http://sstatic.net/so/apple-touch-icon.png"&gt;
</html>

$ awk 'BEGIN{RS="</title>"}/title/{gsub(".*<title>","");print}' file
How to extract a page
title - Stack Overflow
ghostdog74