本文介绍了unix tr查找并替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我从网站wget
的标准网页上使用的命令.
This is the command I'm using on a standard web page I wget
from a web site.
tr '<' '\n<' < index.html
但是它给了我换行符,但是没有添加左边的残破符.例如
however it giving me newlines, but not adding the left broket in again.e.g.
echo "<hello><world>" | tr '<' '\n<'
返回
(blank line which is fine)
hello>
world>
代替
(blank line or not)
<hello>
<world>
怎么了?
推荐答案
这是因为tr
仅执行逐字符替换(或删除)操作.
That's because tr
only does character-for-character substitution (or deletion).
改为尝试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<")'
这篇关于unix tr查找并替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!