cat 1.html | grep \"\" > title.txt
This grep statement is not working.
Please tell the best way to grab the title of a pa
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
sed -n 's/<title>\(.*\)<\/title>/\1/Ip' 1.html
uses the combination of -n and p to only print matches
cat 1.html | grep -oE "<title>.*</title>" | sed 's/<title>//' | sed 's/<\/title>//'
Grep with -oE extract only the title tag, then sed remove the html tags
You can use xml_grep from the XML::Twig Perl package:
xml_grep --text_only title 1.html
Alex Hovansky's answer is good enough, although there is a chance that html is not well formed and your xml_grep would crash
I recommend use tidy to convert html to xml, then use xml_grep
tidy -asxml -utf8 html_file.html > out.xml
xml_grep 'xpath_expression' out.xml
grep "<title>" /path/to/html.html
Works fine for me. Are you sure 1.html is in your current working directory? pwd
to check.