LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack 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,778
Blog Entries: 4

Rep: Reputation: 282Reputation: 282Reputation: 282
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: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
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,778
Blog Entries: 4

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
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,778
Blog Entries: 4

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
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
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 1,737

Rep: Reputation: 184Reputation: 184
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
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
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,778
Blog Entries: 4

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
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: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 03:35 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration