我很難過。我有一個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'有更好的運氣,因爲這其中有你處理,你可以填入實際變量該文件,然後寫在最後。 –