如何使用sed提取特定的子串?

How to use sed to extract the specific substring?

div class="panel-body" id="current-conditions-body">
    <!-- Graphic and temperatures -->
    <div id="current_conditions-summary" class="pull-left" >
                    <img src="newimages/large/sct.png" alt="" class="pull-left" />
                    <p class="myforecast-current">Partly Cloudy</p>
        <p class="myforecast-current-lrg">64&deg;F</p>
        <p class="myforecast-current-sm">18&deg;C</p>

我尝试提取第 6 行中的“64”,我想使用 awk '/<p class="myforecast-current-lrg">/{print}',但这只给了我完整的行。然后我想我需要使用sed,但我不知道如何使用sed

假设:

  • 根据 OP 提供的示例,输入格式很好,因此我们可以使用一些 'simple' 模式匹配

修改OP的当前awk代码:

# use split() function to break line using dual delimiters ">" and "&"; print 2nd array entry

awk '/<p class="myforecast-current-lrg">/{ n=split([=10=],arr,"[>&]");print arr[2]}'

# define dual input field delimiter as ">" and "&"; print 2nd field in line that matches search string

awk -F'[>&]' ' /<p class="myforecast-current-lrg">/{print }'

这两个生成:

64

一个sed想法:

sed -En 's/.*<p class="myforecast-current-lrg">([^&]+)&deg.*//p'

这会生成:

64