LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Command to arrange vertical list in a horizontal manner (https://www.linuxquestions.org/questions/linux-newbie-8/command-to-arrange-vertical-list-in-a-horizontal-manner-4175432150/)

shivaa 10-14-2012 10:24 AM

Command to arrange vertical list in a horizontal manner
 
Let's say, I have a file, containing following entries:
Jack
Smith
William
Rose
Peter
.....


And I want to arrange these entries in a horizontal manner, seperated by space or any other seperator, like this:
Jack Smith William Rose Peter....
So how can I achieve this. Please suggest any simple command or awk script.
Thanks in advance!

David the H. 10-14-2012 10:50 AM

Code:

tr '\n' ' ' <file.txt

grail 10-14-2012 11:14 AM

Code:

awk 'ORS=" "' file

shivaa 10-14-2012 12:11 PM

Quote:

Originally Posted by David the H. (Post 4805432)
Code:

tr '\n' ' ' <file.txt

It's working, but at the end of the output, it's printing the prompt as well, like this:
Jack Smith William Rose Peter user@example.com
So how to seperate the output and prompt, so it print the prompt in new line?

shivaa 10-14-2012 12:12 PM

Quote:

Originally Posted by grail (Post 4805445)
Code:

awk 'ORS=" "' file

It's working, but at the end of the output, it's printing the prompt as well, like this:
Jack Smith William Rose Peter user@example.com
So how to seperate the output and prompt, so it print the prompt in new line?

teckk 10-14-2012 02:19 PM

Code:

awk 'ORS=" "' file.txt &&
Then press enter after it's done.
or
Code:

awk 'ORS=" "' /home/teckk/SteelWeight.txt && ' '
or
Code:

awk 'ORS=" "' /home/teckk/SteelWeight.txt && echo done
Or 10 other ways I guess.

grail 10-14-2012 11:10 PM

I am not sure if you are asking a question there or not?

If you are using &&, you will need to provide the next task, ie. ' ' would not be appropriate but your echo should be fine.

However, I would point out that unless there is an error in your awk script code it will never not go past && as it always returns true.

shivaa 10-14-2012 11:34 PM

Quote:

Originally Posted by teckk (Post 4805582)
Code:

awk 'ORS=" "' file.txt &&
Then press enter after it's done.
or
Code:

awk 'ORS=" "' /home/teckk/SteelWeight.txt && ' '
or
Code:

awk 'ORS=" "' /home/teckk/SteelWeight.txt && echo done
Or 10 other ways I guess.

Yes, putting && echo is giving dessired output,
awk 'ORS=" "' filename && echo
tr '\n' ' ' filename && echo

David the H. 10-15-2012 12:18 AM

Quote:

Originally Posted by meninvenus (Post 4805480)
It's working, but at the end of the output, it's printing the prompt as well...

The command itself is not printing the prompt. Because all newline characters are converted, including the final one, when the prompt returns after the command finishes it simply shows up right after the output. If you redirected the output into a file, for example, the prompt line will not show up in it.

The same thing will happen with any output that does not include a trailing newline.

As shown, following up with a simple && echo solves that problem.

maddyfreaks 07-28-2016 05:59 PM

paste -d, -s <file>

here , is my delimiter.


All times are GMT -5. The time now is 04:46 PM.