我在plan9port環境shell寫到這,但它的端口容易任何Linux。
這段代碼創建了一個sed腳本來控制你的數據。
#!/usr/local/plan9/bin/rc
# .style1:color:#FFEFA1
cat > this.sed <<EOF
# for lines which start with .
/\./{
# strip open curly brace
s, {,:,
# store element tag
h
# skip to next line
n
}
# strip close curly brace
/}/d
# for other lines
{
# remove spaces
s, ,,g
# get rid of ; at end
s,;$,,g
# pull back in the element tag
G
# join to one line
s,\n,,
# shift element tag to the start
# sed in plan 9 is a little different
# for gnu sed, use \(\) and \+
s,(.*)(\.[^.]+$),\2\1,
# finally print something
p
}
EOF
這段代碼運行你對sed腳本輸入,
cat | sed -n -f this.sed <<EOF
.style1 {
font-size: 24px;
font-weight: bold;
color: #FFEFA1;
}
EOF
生成此輸出。
.style1:font-size:24px
.style1:font-weight:bold
.style1:color:#FFEFA1
你可以grep你想要的行,或者「grep -v」那些你不需要的行。
我注意到第一個大括號是},不應該是{? 另外,它怎麼不工作?哪些部件匹配,哪些部件不匹配? – whatsisname 2009-06-05 21:59:50
你說得對。我改變了支架,它仍然不起作用。 我想也許我可能需要別的東西...... 也許就像「如果這行有一個十六進制代碼或括號上,然後不刪除它,但刪除一切,但我不是確定如何對代碼進行編碼 – goddamnyouryan 2009-06-05 22:21:29
請注意,這正是使用正則表達式給其他程序員帶來的問題的類型。解析是要走的路,CPAN已經有'CSS'(死簡單的API)或'CSS :: SAC'。 – hao 2009-06-05 22:55:54