Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to know if there is a way to list all the html tags in a file. Lets say I have a file file.html

<html>
<head>
<title>Test</title>
</head>
<body>
This is a test
</body>
</html>

And I want to get a list of all the tags. That is:

<html>
<head>
<title>
</title>
</head>
<body>
</body>
</html>

I tried using sed,

cat file.html | sed 's/<[^>]*>//g'

But it removed all the html tags insted . . . .

share|improve this question
3  
WHY GOD, WHY? –  Braiam Jul 23 at 20:19
    
I have read that before. . . But there has to be a way v.v –  Unknown Jul 23 at 20:22
    
Which shell? bash? –  Cyrus Jul 23 at 21:02

2 Answers 2

up vote 3 down vote accepted

A quick hack with perl:

perl -wlne 'print for(/<.*?>/g)' file.html

But for a serious solution you should use a tool that really understands html/xml.

share|improve this answer
    
I need a bash solution only :( sorry about that:/ –  Unknown Jul 23 at 20:24
1  
Be aware that your sed solution is not bash only... –  michas Jul 23 at 20:25
    
umm didnt know that :/ But your solution is so perfect :/ –  Unknown Jul 23 at 20:26
1  
grep -oP '<.*?>' filename –  Cyrus Jul 23 at 20:33
    
@Cyrus also not bash-only... –  derobert Jul 23 at 20:35

Using an actual html parser isn't that hard:

perl -MHTML::Parser -E '
  $handler = sub {say "<".shift.">"};
  HTML::Parser->new(start_h => [$handler,"tag"], end_h => [$handler,"tag"])
              ->parse_file(shift @ARGV)
' file.html
<html>
<head>
<title>
</title>
</head>
<body>
</body>
</html>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.