这是我在网上从我wget的标准网页上使用的命令。unix tr查找并替换
tr '<' '\n<' < index.html
但是它给了我换行符,但没有再添加回胡萝卜。 例如
echo "<hello><world>" | tr '<' '\n<'
回报
(blank line which is fine)
hello>
world>
,而不是
(blank line or not)
<hello>
<world>
感谢
这是我在网上从我wget的标准网页上使用的命令。unix tr查找并替换
tr '<' '\n<' < index.html
但是它给了我换行符,但没有再添加回胡萝卜。 例如
echo "<hello><world>" | tr '<' '\n<'
回报
(blank line which is fine)
hello>
world>
,而不是
(blank line or not)
<hello>
<world>
感谢
这是因为tr
只做字符的字符替换(或删除)。
改为尝试sed
。
echo '<hello><world>' | sed -e 's/</\n&/g'
或awk
。
echo '<hello><world>' | awk '{gsub(/</,"\n<",$0)}1'
或perl
。
echo '<hello><world>' | perl -pe 's/</\n</g'
或ruby
。
echo '<hello><world>' | ruby -pe '$_.gsub!(/</,"\n<")'
或python
。
echo '<hello><world>' \
| python -c 'for l in __import__("fileinput").input():print l.replace("<","\n<")'
这是否适合您?
awk -F"><" -v OFS=">\n<" '{print $1,$2}'
[jaypal:~/Temp] echo "<hello><world>" | awk -F"><" -v OFS=">\n<" '{$1=$1}1';
<hello>
<world>
你可以把在awk
{}
行动的前一个正则表达式/ /(你希望这种情况发生的线)。
''{$ 1 = $ 1} 1''较短,如果一行中有多于'><'的话就会工作。 – ephemient
谢谢@ephemient我同意,已更新我的答案。 –
这会取代较少的'<'字符。 –
如果你有GNU grep
,这可能会为你工作:
grep -Po '<.*?>[^<]*' index.html
应该通过所有的HTML,但每个标签应与可能的非标记文本行的开头以下启动在同一行上。
如果你想什么,但标签:
grep -Po '<.*?>' index.html
你应该知道,但是,它的not a good idea用正则表达式解析HTML。
您放置换行符的顺序很重要。你也可以逃脱“<”。
tr '\/<' '\/<\n' < index.html
`tr '<' '<\n' < index.html` works as well.
我想,但我得到n个ñ。我不知道什么是sed换行符是 –
Kamran224
@ Kamran224这适用于我,但尝试:echo -e''| sed -e's/ \ n&/ g' –
2011-12-01 23:29:39
@ Kamran224'\ n'是一个GNU sed扩展。你在做什么系统? – ephemient