Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file containing lines having this structure :

var marker25 = createMarker(point, '<div id="infowindow" style="white-space: nowrap;"><h3>Katz\'s Deli</h3>205 E. Houston Street, Manhattan, New York City,<br />New York, USA<br /><br /> <a href="/When_Harry_Met_Sally/filming_locations"><img style="margin-right: 5px; float: left; width: 100px; height: 150px" src="/images/posters/64-title.jpg" /></a></div>');
var marker26 = createMarker(point, '<div id="infowindow" style="white-space: nowrap;"><h3>Rockerfeller Roof Gardens</h3>5th Avenue between 49th &amp; 50th Streets, Manhattan, New York City,<br />New York, USA<br /><br /> <a href="/Spider-man/filming_locations"><img style="margin-right: 5px; float: left; width: 100px; height: 150px" src="/images/posters/8-title.jpg" /></a></div>');

I want to quickly be able to have as a result line of this type:

Katz\'s Deli 205 E. Houston Street, Manhattan, New York City,New York, USA,When_Harry_Met_Sally

Rockerfeller Roof Gardens,5th Avenue between 49th &amp; 50th Streets,,New York, USA, Spider-man

To reiterate, I want to remove all HTML/JS strings to keep only addresses, country and so on.

What I did so far is :

grep 'createMarker' New_York_Filming_Locations.php 
cut -d'<' -f1,2,3,4,5,6,7,8  
sed 's/<br \/>/,/g;s/<h3>/,/g;s/<\/h3>/,/g' 
cut -d',' -f3,4,5,7,8,9,11,12 
sed 's/<a href="\///g;s/\/filming_locations">//g' 
cut -d',' -f1,2,4,5,6,7

It is done in a pipe, but for the clarity of the questions, I put each command on a separate line.

Is this the best way to do it? It is a small file, so I do not care about performance.

The sed syntax can be hard to understand. Do you guys know another way to do it using bash?

share|improve this question
Bash, sed and regexps are notoriously not suited for parsing html. Use a specialized library in another language instead. – gniourf_gniourf Dec 25 '12 at 20:37
@gniourf_gniourf, I guess for parsing php it is even less suitable. But as I understand this is expecting to be fragile. – ony Dec 25 '12 at 20:44
It is just to parse a really small file quickly, without having to use any library or anything. I totally know that it is not suitable, but it is possible. I am interested in the how, not the why :) – Jeremy D Dec 25 '12 at 23:11
up vote 2 down vote accepted

Consider removing tags with

sed 's/<[^>]*>/ /g'
share|improve this answer

Your Answer


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.