Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Title of a html file using grep

Tags:

grep

bash

shell

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

This grep statement is not working.

Please tell the best way to grab the title of a page using grep or sed.

Thanks.

like image 805
Vamsi Krishna B Avatar asked Dec 04 '25 15:12

Vamsi Krishna B


1 Answers

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">
<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
like image 161
ghostdog74 Avatar answered Dec 07 '25 05:12

ghostdog74



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!