如何结合两个SED regex命令?

时间:2019-09-19 14:17:52

标签: bash sed

很遗憾,我已经阅读了我可以在SOF上找到的所有答案,但是没有一个答案让我找到了解决方案。 我有成千上万个包含地址信息的文件,并且我的每个SED命令都可以独立工作

比赛地址

sed -n -e 's/^.*address23ca storeh2..\(.*\) Address<\/h2.*optmob..\(.*\)<br>\(.*\)<br>\(.*\)<br>\(.*\)<\/p><p class="addressbox23.*Telephone: \(.-...-...-....\)\(.*\).*v1\/place?..\(.*\)&key.*$/\1,\2,\3,\4,\5,\6/p' afile.html

$ 142 Wayne Street,Abbey,Saskatchewan,S0N 0A0,1-232-321-4321

匹配GPS

sed -n -e 's/^.*v1\/place?..\(.*\)&key.*$/\1/p' abbey.html

$ 50.736301,-108.757103

我尝试了以下操作,但是在电话号码之后它并没有停止匹配,而是一直持续到匹配v1\/place?之后才停止。我不知道如何在电话号码处停止匹配并重新开始GPS匹配。

如何将这两场比赛结合起来?

sed -n -e 's/^.*address23ca storeh2..\(.*\) Address<\/h2.*optmob..\(.*\)<br>\(.*\)<br>\(.*\)<br>\(.*\)<\/p><p class="addressbox23.*Telephone: \(.-...-...-....\)\(.*\).^*v1\/place?..\(.*\)&key.*$/\1,\2,\3,\4,\5,\6,\7/p' afile.html

$ 142 Wayne Street,Abbey,Saskatchewan,S0N 0A0,1-232-321-4321 LOADS OF unnecessary HTML src="https://www.google.com/maps/embed

文件的修整版本

<!DOCTYPE html>
<html lang="en"> <!--<![endif]-->
<head></head><body> <div class="large-7 columns small-12 addWrap23ca"> <div class="storeH2Wrap23ca"> <h2 class="address23ca storeh2">Canada Post Abbey Address</h2></div><p class="addressbox23ca optmob">142 Wayne Street<br>Abbey<br>Saskatchewan<br>S0N 0A0</p><p class="addressbox23ca optmob">Telephone: 1-866-607-6301</p></div></div><div class="row"> <div class="large-12 medium-12 columns small-12"> <div class="row"> <div class="large-12 columns small-12"> <div class="storeH2Wrap23ca"> <h2 class="hours23ca storeh2">Canada Post Abbey Opening Hours</h2></div><div class="hoursCont23ca"> 13:00-16:30</p><p>Closed</p><p>Closed</p></div></div><div class="notesWrap23ca"><div class="notesTitle23ca"><p class="noteHeading23ca">Post Office Notes</p></div><div class="notesContent23ca"><p class="note23ca">This Post Office Branch closes for lunch on certain days - please see opening hours.</p></div></div></div></div></div></div><div class="row"> <div class="mapadCont23ca"> <div class="large-12 medium-12 columns small-12 map23ca"> <div class="storeH2Wrap23ca storeH2WrapMap23ca"> <h2 class="maptitle23ca storeh2">Canada Post Abbey Map Location</h2></div><div class="mapBreadCrumbs23ca"><ul><li><a href="../canada-post/canada-post.html">Canada Post Locator</a></li><li>&gt;</li><li><a href="saskatchewan.html">Canada Post Saskatchewan</a></li><li>&gt;</li><li>Canada Post in Abbey</li></ul></div> <div class="mapCont23ca"> <iframe width="100%" height="434" frameborder="0" src="https://www.google.com/maps/embed/v1/place?q=50.736301,-108.757103&key=AIzaSyDmJApckRpAR1uhfdfz_QedneaF5lAlrQU"></iframe></div><div class="searchagainouter23ca"> <div class="adddivclear" style="clear:both;"></div></body></html>

2 个答案:

答案 0 :(得分:0)

您可以将两个正则表达式与分号结合使用

$ echo "etts" | sed 's/et/te/; s/ts/st/'
test

答案 1 :(得分:0)

您可以使用更好的工具来完成工作,例如python的HTMLParser。这是打印找到的所有标签的示例,您可以在其中添加所需的任何过滤器

#! /usr/bin/env python3
from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print("Found a start tag:", tag)
        print("\tattrs:", attrs)
    def handle_endtag(self, tag):
        print("Found an end tag:", tag)
    def handle_data(self, data):
        print("Found data:", data)

MyHTMLParser().feed('''
<!DOCTYPE html>
<html lang="en"> <!--<![endif]-->
<head></head><body> <div class="large-7 columns small-12 addWrap23ca"> <div class="storeH2Wrap23ca"> <h2 class="address23ca storeh2">Canada Post Abbey Address</h2></div><p class="addressbox23ca optmob">142 Wayne Street<br>Abbey<br>Saskatchewan<br>S0N 0A0</p><p class="addressbox23ca optmob">Telephone: 1-866-607-6301</p></div></div><div class="row"> <div class="large-12 medium-12 columns small-12"> <div class="row"> <div class="large-12 columns small-12"> <div class="storeH2Wrap23ca"> <h2 class="hours23ca storeh2">Canada Post Abbey Opening Hours</h2></div><div class="hoursCont23ca"> 13:00-16:30</p><p>Closed</p><p>Closed</p></div></div><div class="notesWrap23ca"><div class="notesTitle23ca"><p class="noteHeading23ca">Post Office Notes</p></div><div class="notesContent23ca"><p class="note23ca">This Post Office Branch closes for lunch on certain days - please see opening hours.</p></div></div></div></div></div></div><div class="row"> <div class="mapadCont23ca"> <div class="large-12 medium-12 columns small-12 map23ca"> <div class="storeH2Wrap23ca storeH2WrapMap23ca"> <h2 class="maptitle23ca storeh2">Canada Post Abbey Map Location</h2></div><div class="mapBreadCrumbs23ca"><ul><li><a href="../canada-post/canada-post.html">Canada Post Locator</a></li><li>&gt;</li><li><a href="saskatchewan.html">Canada Post Saskatchewan</a></li><li>&gt;</li><li>Canada Post in Abbey</li></ul></div> <div class="mapCont23ca"> <iframe width="100%" height="434" frameborder="0" src="https://www.google.com/maps/embed/v1/place?q=50.736301,-108.757103&key=AIzaSyDmJApckRpAR1uhfdfz_QedneaF5lAlrQU"></iframe></div><div class="searchagainouter23ca"> <div class="adddivclear" style="clear:both;"></div></body></html>
''')