LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-14-2012, 10:24 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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!

Last edited by shivaa; 10-14-2012 at 10:25 AM.
 
Old 10-14-2012, 10:50 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Code:
tr '\n' ' ' <file.txt
 
Old 10-14-2012, 11:14 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
awk 'ORS=" "' file
 
Old 10-14-2012, 12:11 PM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800

Original Poster
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by David the H. View Post
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?

Last edited by shivaa; 10-14-2012 at 12:13 PM.
 
Old 10-14-2012, 12:12 PM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800

Original Poster
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by grail View Post
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?
 
Old 10-14-2012, 02:19 PM   #6
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,136
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
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.
 
Old 10-14-2012, 11:10 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 10-14-2012, 11:34 PM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800

Original Poster
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by teckk View Post
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

Last edited by shivaa; 10-14-2012 at 11:35 PM.
 
Old 10-15-2012, 12:18 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by meninvenus View Post
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.
 
Old 07-28-2016, 05:59 PM   #10
maddyfreaks
Member
 
Registered: May 2011
Posts: 85

Rep: Reputation: 0
paste -d, -s <file>

here , is my delimiter.
 
  


Reply

Tags
awk, command


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] horizontal to vertical dinakumar12 Linux - Newbie 3 03-13-2012 03:55 PM
[SOLVED] splitting of comma separated values file (horizontal) into a list (vertical ) list Donoughue Linux - Newbie 3 10-20-2011 01:18 PM
Horizontal and Vertical sync rates?? chris24300 Linux - Newbie 5 09-08-2009 04:18 PM
NVIDIA - Horizontal/Vertical Adjust JamboUK Ubuntu 4 05-19-2006 05:52 AM
Horizontal and vertical refresh sharpie General 5 01-24-2004 07:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration