我很难过。我有一个HTML文件,我试图将其转换为纯文本,并使用sed
进行清理。据我所知,sed
适用于'流',一次只能处理一行,但有多种方法可以匹配多行模式。
这里是我的源文件的相关部分:Sed程序 - 删除的字符串重新出现?
<h1 class="fn" id="myname">My Name</h1>
<span class="street-address">123 street</span>
<span class="locality">City</span>
<span class="region">Region</span>
<span class="postal-code">1A1 A1A</span>
<span class="email">[email protected]</span>
<span class="tel">000-000-0000</span>
我想这个被制作成以下明文格式:
My Name
123 street
City Region 1A1 A1A
[email protected]
000-000-0000
的关键在于,城市,地区和邮政编码现在都在一条线上。
我用sed -f commands.sed file.html > output.txt
,我相信下面的sed程序(),应该把它放在格式:
#using the '@' symbol as delimiter instead of '/'
#remove tags
[email protected]<.*>\(.*\)</.*>@\[email protected]
#remove the nbsp
[email protected]\( \)*@@g
#add a newline before the address (actually typing a newline in the file)
[email protected]\(123 street\)@\
\[email protected]
#and now the command that matches multiline patterns
#find 'City',read in the next two lines, and separate them with spaces
/City/ {
N
N
[email protected]\(.*\)\n\(.*\)\n\(.*\)@\1 \2 \[email protected]
}
似乎是有道理的。标签全部被剥离,然后三条线被放入一个。
Buuuuut它不会那样工作。下面是结果我得到:
My Name
123 street
City <span class="region">Region</span> <span class="postal-code">1A1 A1A</span>
[email protected]
000-000-0000
要我(相对缺乏经验的)眼睛,它看起来像sed是“遗忘”它所做的更改(剥去标签)。我将如何解决这个问题?解决方案是在三条命令之后写入文件,并重新运行sed以获得第四条命令?我滥用sed吗?我误解了'流'部分?
我使用bash
外壳程序运行Mac OS X 10.4.11,并使用它随附的sed
版本。
你可能使用'awk'有更好的运气,因为这其中有你处理,你可以填入实际变量该文件,然后写在最后。 –