2

I have a file that is formatted like this:

> ABC
1
2
> DEF
3
4

I would like to use tr to replace each > with 4 carriage returns, so it looks like:

 ABC
1
2




 DEF
3
4

I tried the following in the Terminal: cat input | tr ">" "\n\n\n\n" > output However, this only adds one carriage return between the two blocks of data, like this:

 ABC
1
2

 DEF
3
4

How can I get it to recognize the multiple carriage returns? Thanks!

4

1 回答 1

2

tl;博士

tr 是错误的工作工具;尝试别的东西(比如 sed)


tr (文本替换)仅进行 1:1 替换 - 因此它一次只会替换一个字符。我认为您当前的命令将 > 替换为 /n,>> 替换为 /n/n,>>> 替换为 /n/n/n,>>>> 替换为 /n/n/n/n。

尝试使用 sed 代替,可能是这样的(未经测试!):

cat input | sed $'s/>/\\\n\\\n\\\n\\\n/g' > output
于 2013-08-21T16:18:12.987 回答