How to remove multiline HTML tags from a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove multiline HTML tags from a file?
# 1  
Old 09-08-2015
How to remove multiline HTML tags from a file?

I am trying to remove a multiline HTML tag and its contents from a few HTML files following the same basic pattern. So far using regex and sed have been unsuccessful. The HTML has a basic structure like this (with the normal HTML stuff around it):
Code:
<div id="div1">
 <div class="div2">
  <other random tags here></other random tags here>
 </div><a id="a1" href="#"><span>Some text</span></a>
</div>

I would like to remove this tag and its contents but I don't know how using scripts or command line programs.
# 2  
Old 09-08-2015
Hello threesixtyfive,

Welcome to forum, a special thanks for using code tags for inputs/html codes in your posts Smilie. Following may help you in same.
Let's say we have following input file:
Code:
cat test12121344
<html>
<title>
test
</title>
<body>
I am testing here, R. Singh
<div id="div1">
 <div class="div2">
  <other random tags here></other random tags here>
 </div><a id="a1" href="#"><span>Some text</span></a>
</div>
</body>
</html>

Now following code may help you in same.
Code:
awk '{if($0 ~ /<div id=\"div1\">/){getline;if($0 ~ / <div class=\"div2\">/){getline;if($0 ~ /  <other random tags here><\/other random tags here>/){getline;if($0 ~ / <\/div><a id=\"a1\" href=\"#\"><span>Some text<\/span><\/a>/){getline;if($0 ~ /<\/div>/){next}}}}}}{print}' test12121344

Output will be as follows.
Code:
 <html>
<title>
test
</title>
<body>
I am testing here, R. Singh
</body>
</html>

But please make sure that each html file which you are using this code must have same data as you shown else we need to modify the code accordingly, hope this helps. Following is code which it will NOT catch in html files.
Code:
<div id="div1">
 <div class="div2">
  <other random tags here></other random tags here>
 </div><a id="a1" href="#"><span>Some text</span></a>
</div>

EDIT: Adding a non one-liner form of solution here.
Code:
 awk '{if($0 ~ /<div id=\"div1\">/){
                                        getline;
                                        if($0 ~ / <div class=\"div2\">/){
                                                                        getline;if($0 ~ /  <other random tags here><\/other random tags here>/){
                                                                        getline;if($0 ~ / <\/div><a id=\"a1\" href=\"#\"><span>Some text<\/span><\/a>/){
                                                                        getline;if($0 ~ /<\/div>/){next}
                                                                                                                                                        }
                                                                                                                                                        }
                                                                        }
                                  }
      }
      {
        print
      }
    ' test12121344

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-08-2015 at 01:26 PM.. Reason: Added a non one-liner form of solution now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-08-2015
What I meant by the
Code:
<other random tags here></other random tags here>

was that there would be other divs, a span, or just nothing. How can I make it so where the <other random tags here> is, it just accepts anything until it finds those last to HTML tags specified by awk?

Last edited by threesixtyfive; 09-08-2015 at 02:09 PM..
# 4  
Old 09-08-2015
Hello threesixtyfive,

Could you please try following and let me know if this helps you.
Code:
awk 'BEGIN{A=1}/<\/div>/{A=1;next} /<div.*>/{A=0}; A{print}' test12121344

Where test12121344 is the Input_file as follows.
Code:
 cat test12121344
<html>
<title>
test
</title>
<body>
I am testing here, R. Singh
<div id="div1">
 <div class="div2">
  <other random tags here></other random tags here>
 </div><a id="a1" href="#"><span>Some text</span></a>
</div>
</body>
</html>

Following will be output after running code.
Code:
 awk 'BEGIN{A=1}/<\/div>/{A=1;next} /<div.*>/{A=0}; A{print}' test12121344
<html>
<title>
test
</title>
<body>
I am testing here, R. Singh
</body>
</html>

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-08-2015 at 03:07 PM.. Reason: Adding the output after running code.
# 5  
Old 09-08-2015
Try
Code:
awk '
/<div id="div1">/       {P=1
                         next
                        }
/<div/ && P             {P++       
                         next
                        }
/<\/div/ && P           {P--       
                         next
                        }
!P
' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiline html tag parse shell script

Hello, I want to parse the contents of a multiline html tag ex: <html> <body> <p>some other text</p> <div> <p class="margin-bottom-0"> text1 <br> text2 <br> <br> text3 </p> </div> </body> (15 Replies)
Discussion started by: SorcRR
15 Replies

2. Shell Programming and Scripting

How to remove the values inside the html tags?

Hi, I have a txt file which contain this: <a href="linux">Linux</a> <a href="unix">Unix</a> <a href="oracle">Oracle</a> <a href="perl">Perl</a> I'm trying to extract the text in between these anchor tag and ignoring everything else using grep. I managed to ignore the tags but unable to... (6 Replies)
Discussion started by: KCApple
6 Replies

3. Shell Programming and Scripting

sed failing with multiline html loaded with metacharacters

I have html files (newlines ending in linefeed) with metacharacter-laden multiline text I want to replace. To matters more complicated, the first line may appear elsewhere in the file, so I need these lines as a block. I can replace individual lines, but am failing to come up with anything that can... (2 Replies)
Discussion started by: mauricev
2 Replies

4. Shell Programming and Scripting

Removing all except couple of html tags from html file

I tried to find elegant (or at least simple) way to remove all but couple of html tags from html file, but all examples I found dealt with removing all the tags. The logic of the script would be: - if there is <li> or <ul> on the line, do nothing (=write same line to output) - if there is:... (0 Replies)
Discussion started by: juubuntu
0 Replies

5. Shell Programming and Scripting

Remove html tags with particular string inside the tags

Could someone, please provide a solution to the following: I would like to remove some tags from the "head" of multiple html documents across the web site. They look like <link rel="alternate" type="application/rss+xml" title="Business and Investment in the Philippines"... (2 Replies)
Discussion started by: georgi58
2 Replies

6. Shell Programming and Scripting

remove html tags,consecutive duplicate lines

I need help with a script that will remove all HTML tags from an HTML document and remove any consecutive duplicate lines, and save it as a text document. The user should have the option of including the name of an html file as an argument for the script, but if none is provided, then the script... (7 Replies)
Discussion started by: clicstic
7 Replies

7. Shell Programming and Scripting

Remove external urls from .html file

Hi everyone. I have an html file with lines like so: link href="localFolder/..."> link href="htp://..."> img src="localFolder/..."> img src="htp://..."> I want to remove the links with http in the href and imgs with http in its src. I'm having trouble removing them because there... (4 Replies)
Discussion started by: CowCow339
4 Replies

8. Shell Programming and Scripting

How to use sed to remove html tags including text between them

How to use sed to remove html tags including text between them? Example: User <b> rolvak </b> is stupid. It does not using <b>OOP</b>! and should output: User is stupid. It does not using ! Thank you.. (2 Replies)
Discussion started by: alphagon
2 Replies

9. Shell Programming and Scripting

Remove html tags with bash

Hello, is there a way to go through a file and remove certain html tags with bash? If it needs sed or awk, that'll do too. The reason why I want this is, because I have a monitor script which generates a logfile in HTML and every time it generates a logfile, the tags are reproduced. The tags... (4 Replies)
Discussion started by: dejavu88
4 Replies

10. Linux

How to remove only html tags inside a file?

Hi All, I have following example file i want to remove all html tags only, Input File: <html> <head> <title>Software Solutions Inc., </title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor=white leftmargin="0" topmargin="0"... (2 Replies)
Discussion started by: btech_raju
2 Replies
Login or Register to Ask a Question