unix tr查找并替换 [英] unix tr find and replace

查看:80
本文介绍了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查找并替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆