LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 10: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, 11:04 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,808

Rep: Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604Reputation: 604
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, 11:05 AM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

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

Rep: Reputation: 45
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, 11: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, 11: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, 11:36 AM   #7
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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, 10:45 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Wouldn't be complete without an awk entry:
Code:
awk '1;END{print "\n"}' ORS=" " file
 
Old 02-01-2011, 11: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


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


All times are GMT -5. The time now is 06:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration