CyberKeeda In Social Media

BASH: How to transform Vertical list into Horizontal list



This one liner will definitely help you, as i too believe this is one of the common requirement in order to solve complex requirements of various shell scripts.

While developing a script , i got a requirement to transform all vertical strings appended one after another into a single horizontal line.

Here is the example to make you understand my requirement and the end result as STDOUT.

[root@cyberkeeda.com ~]# cat myfile.txt
Hello World !
This
is 
my
blog
cyberkeeda.com
So i want it to look like  the below one

Hello World ! This is my blog cyberkeeda.com

There are multiple ways and tools to accomplish the same task, but i found  tr  as the best and short one.

[root@cyberkeeda.com ~]# cat myfile.txt |  tr '\n' ' '

I will be posting multiple ways later into the same post , to share more ways to do that.


No comments:

Post a Comment

Designed By Jackuna