Using tr to replace newline with space [duplicate]
Asked Answered
S

1

93

Have output from sed:

http://sitename.com/galleries/83450
72-profile

Those two strings should be merged into one and separated with space like:

http://sitename.com/galleries/83450 72-profile

Two strings are pipelined to tr in order to replace newline with space:

tr '\n' ' '

And it's not working, the result is the same as input.

Indicating space with ASCII code '\032' results in replacing \n with non-printable characters.

What's wrong? I'm using Git Bash on Windows.

Selfevident answered 13/9, 2014 at 19:17 Comment(2)
I tested piping a text file with newlines to tr with the same syntax and it works. How are you doing the pipe? Does the file have newlines AND linefeeds? maybe try "tr '\r\n' ' '"Forb
Why is this marked as a duplicate? The linked question specifically asked using sed, which is not what this question is about (using tr). Though the goal is the same, there may be a lot of reasons why the asker or a viewer would like to use tr and tr only.Ungrudging
F
149

Best guess is you are on windows and your line ending settings are set for windows. See this topic: How to change line-ending settings

or use:

tr '\r\n' ' '
Forb answered 13/9, 2014 at 19:36 Comment(2)
agree about windows line endings. The quick fix is dos2unix file. Good luck to all.Bowlds
well the script still has '\r'. But it doesn't look like it will work for what you want. It will put everything on one line, not just 2 lines at a time, and then your FOR loop will always go through just once.Forb

© 2022 - 2024 — McMap. All rights reserved.