Liju Thomas Liju Thomas - 2 months ago 14
Bash Question

Convert vertical text into horizontal in shell

I just pondered upon this problem while executing some tasks in shell.

How can I convert a

.txt
file which contains text in the following format[vertical direction]:

H
e
l
l
o

W
o
r
l
d



Note: The line without any letter contains a space and EOL


I need to convert to:

Hello World


I tried the following command:
cat file1.txt |tr '\n' ' ' >file2.txt


Output:

H e l l o W o r l d


How can I proceed further? Sorry, if this question has already appeared. If so, please provide me the link to the solution or any command for the work around.

Answer

After going through all the suggestions and little bit of research, I found a solution for this, wrote a script for the same:

#!/bin/bash
#Converting text to horizontal direction

IFS=''
while read line
do
   echo -n $line
done < $1 > $2           # $1 --> file1.txt contain vertical text as mentioned in the question
echo "$(cat $2)"

Command:

$./convert.sh file1.txt file2.txt

Output:

Hello World
Comments