LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 02-01-2011, 09:59 AM   #1
saini_mw
Member
 
Registered: Jun 2005
Location: On the top of the World
Posts: 114

Rep: Reputation: 15
want to list output in single line (space separated)


Hello Guys!

got a situation where if i process a command i get a numeric output something like below:

for example:

# cat example.txt
856589
856470
987866
656658
876897

now i want to see this output in single line like below:

856589 856470 987866 656658 876897

how do i get this??

Thanks in advance.
 
Old 02-01-2011, 10:04 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Use 'tr' to replace newlines with spaces

Code:
cat example.txt | tr '\n' ' '
(That \n may need to be a a \r, I can never remember)
 
Old 02-01-2011, 10:05 AM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i think this would do it
Code:
var="`cat example.txt`"
echo var = $var
 
Old 02-01-2011, 10:06 AM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
$ ruby -ne 'print "#{$_.chomp} "' file


---------- Post added 02-01-11 at 11:06 AM ----------

Quote:
Originally Posted by AlucardZero View Post
Use 'tr' to replace newlines with spaces

Code:
cat example.txt | tr '\n' ' '
(That \n may need to be a a \r, I can never remember)
Code:
tr '\n' ' ' < example.txt
 
1 members found this post helpful.
Old 02-01-2011, 10:35 AM   #5
saini_mw
Member
 
Registered: Jun 2005
Location: On the top of the World
Posts: 114

Original Poster
Rep: Reputation: 15
cat example.txt | tr '\n' ' '
856589 856470 987866 656658 876897 [root@host tmp]#

it prints the "[root@host tmp]#" on the same line. is there anyway that i can issue a new line after that???
 
Old 02-01-2011, 10:36 AM   #6
saini_mw
Member
 
Registered: Jun 2005
Location: On the top of the World
Posts: 114

Original Poster
Rep: Reputation: 15
same issue with "tr '\n' ' ' < example.txt"
 
Old 02-01-2011, 10:36 AM   #7
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by saini_mw View Post
cat example.txt | tr '\n' ' '
856589 856470 987866 656658 876897 [root@host tmp]#

it prints the "[root@host tmp]#" on the same line. is there anyway that i can issue a new line after that???
Code:
cat example.txt | tr '\n' ' '
echo ''
 
Old 02-01-2011, 09:45 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Wouldn't be complete without an awk entry:
Code:
awk '1;END{print "\n"}' ORS=" " file
 
Old 02-01-2011, 10:02 PM   #9
vigilandy
Member
 
Registered: Mar 2010
Location: Tokyo
Distribution: Arch, Fedora
Posts: 71

Rep: Reputation: 23
perl to the rescue
Code:
perl -pe 's/\s*$/ /' sample.txt
 
  


Reply



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
how to create a single line of output from multiple variable lines of input steven.c.banks Linux - General 2 02-03-2010 03:09 PM
sorting a list into comma separated list nixlearn Linux - Newbie 22 12-03-2008 06:21 AM
bash: execute a semicolon separated list of commands within string Meson Linux - General 3 10-01-2008 11:25 PM
bash syntax: looping through a comma-separated list David the H. Linux - General 10 09-06-2007 10:23 AM
Application to place single space with % between each line MasterC Linux - General 2 05-18-2003 01:16 PM

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

All times are GMT -5. The time now is 12:57 AM.

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