Print content between two html tags


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print content between two html tags
# 1  
Old 07-13-2015
Print content between two html tags

Hi Expert,

Is there any other way to print and write to a same filename the content between two html tags?

Here the sample:

cat file.html

Code:
<div id="outline">
hello world<br>
</div>
<div id="container_faq">
test1<br>
</div>

<div class="widget_quick">
thead test<br>
</div>

<div id="linkcat-5">
hello<br>
</div>

<div id="left">
first row<br>
second row<br>
third row<br>
<div>
fourth row<br>
fifth row<br></div>
<br>


       <br>
sixth row<br>
seventh row<br>
eight row<br></div>

<br>




<div id="appendix">
hallo<br>
</div>
<div class="widget">
check this<br>
</div>


Code:
sed -n '/<div id="left"/,/<\/div/p' file.html

the output is not what I want
Code:
<div id="left">
first row<br>
second row<br>
third row<br>
<div>
fourth row<br>
fifth row<br></div>

instead the output would be:

Code:
<div id="left">
first row<br>
second row<br>
third row<br>
<div>
fourth row<br>
fifth row<br>
</div>
       <br>
sixth row<br>
seventh row<br>
eight row<br></div>

is there anything to add to the script?

Thanks
# 2  
Old 07-13-2015
Not sure that you could do that with sed. Try this using awk:
Code:
awk '
/<div id=\"left\"/      {P++}

P                       {if (/<div>/)   P++
                         if (/<\/div>/) P--
                         print
                        }
' file
<div id="left">
first row<br>
second row<br>
third row<br>
<div>
fourth row<br>
fifth row<br></div>
<br>


       <br>
sixth row<br>
seventh row<br>
eight row<br></div>

It's not quite what you have as desired output, but as close as you can get without any further playing dirty tricks.
# 3  
Old 07-13-2015
Thanks for this:

I tried the in one line like this:

Code:
awk '/<div id=\"left\"/ {P++} P {if (/<div>/) P++ if (/<\/div>/) P-- print}' file

but is not working and i got this error:

Code:
awk: /<div id=\"left\"/ {P++} P {if (/<div>/) P++ if (/<\/div>/) P-- print}
awk:                                              ^ syntax error
awk: /<div id=\"left\"/ {P++} P {if (/<div>/) P++ if (/<\/div>/) P-- print}
awk:                                                                 ^ syntax error


Thanks
# 4  
Old 07-13-2015
Put semi-colons (;) where the carrot (^) characters are pointing:

Code:
awk '/<div id=\"left\"/ {P++} P {if (/<div>/) P++; if (/<\/div>/) P--; print}' file

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert content of file to HTML

Hi I have file like this: jack black 104 daniel nick 75 lily harm 2 albert 5 and need to convert it into the html table like this: NO.......name....family..... id 1...........jack.....black.....104 2..........daniel....nick.......75 3..........albert.................5 i mean... (5 Replies)
Discussion started by: indeed_1
5 Replies

2. Shell Programming and Scripting

Mailx with attachment and html content

Hi, Please see my code below i'm trying get an email send with attachment and html content in the body. Using the code below will put the encoding for attachment in the body as well SUBJECT="$(echo "XPI Monitoring "${tcnt}" transactions waiting \nContent-Type: text/html")" cat... (3 Replies)
Discussion started by: bhoshan
3 Replies

3. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

4. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

5. 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

6. Shell Programming and Scripting

Parsing HTML, get text between 2 HTML tags

Hi there, I'm quite new to the forum and shell scripting. I want to filter out the "166.0 points". The results, that i found in google / the forum search didn't helped me :( <a href="/user/test" class="headitem menu" style="color:rgb(83,186,224);">test</a><a href="/points" class="headitem... (1 Reply)
Discussion started by: Mysthik
1 Replies

7. Shell Programming and Scripting

help with sed needed to extract content from html tags

Hi I've searched for it for few hours now and i can't seem to find anything working like i want. I've got webpage, saved in file par with form like this: <html><body><form name='sendme' action='http://example.com/' method='POST'> <textarea name='1st'>abc123def678</textarea> <textarea... (9 Replies)
Discussion started by: seb001
9 Replies

8. UNIX for Advanced & Expert Users

sed to extract HTML content

Hiya, I am trying to extract a news article from a web page. The sed I have written brings back a lot of Javascript code and sometimes advertisments too. Can anyone please help with this one ??? I need to fix this sed so it picks up the article ONLY (don't worry about the title or date .. i got... (2 Replies)
Discussion started by: stargazerr
2 Replies

9. UNIX for Dummies Questions & Answers

sending mail with html content

hi, I am new to unix. I need send html content as a mail from my sun-solaris2.6 work station. When I tried that the recipient gets it as html code with all the tags. any solutions? thanx in advance (2 Replies)
Discussion started by: gmchoudary
2 Replies

10. Shell Programming and Scripting

mail: html content

hi guys, am required to prepare a report and mail it, to make it more appealing :p i wish to have content of mail in rich text format i.e html type with mailx how to specify the content type of mail body as html? Thanks in advance!!! rishi (2 Replies)
Discussion started by: RishiPahuja
2 Replies
Login or Register to Ask a Question