Question

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!

Was it helpful?

Solution

tl;dr

tr is the wrong tool for the job; try something else (like sed)


tr (text replace) only does 1:1 replacement - so it will only replace one character, with another, at a time. I think your current command replaces > with /n, >> with /n/n, >>> with /n/n/n and >>>> with /n/n/n/n.

try using sed instead, probably something like this (untested!):

cat input | sed $'s/>/\\\n\\\n\\\n\\\n/g' > output
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top