LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-20-2013, 04:45 AM   #1
nitya
LQ Newbie
 
Registered: Feb 2013
Location: India
Distribution: RedHat, Cent, Fedora, Ubuntu
Posts: 21

Rep: Reputation: Disabled
bash to print words/lines horizontally


Hi All,
I have a test file with below entries
Code:
$ cat /tmp/test 
RedHat
Cent
Fedora
Suse
Ubuntu
Now how can I print these entries horizontally i.e I am expecting the output to be as
Code:
RedHat Cent Fedora Suse Ubuntu
Thanks in advance for your kind help.
 
Old 02-20-2013, 04:54 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Using tr:
Code:
 cat infile | tr "\n" " "
Using awk:
Code:
awk 'BEGIN{ ORS=" " }{ print }' infile
There are probably other ways as well.
 
1 members found this post helpful.
Old 02-20-2013, 05:03 AM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
pure bash:

Code:
foo="$(<infile)"
echo "${foo//$'\n'/ }"
or

Code:
<infile mapfile -t array
echo "${array[@]}"
Of course, there's also Perl, sed and many others.
 
1 members found this post helpful.
Old 02-20-2013, 05:37 AM   #4
nitya
LQ Newbie
 
Registered: Feb 2013
Location: India
Distribution: RedHat, Cent, Fedora, Ubuntu
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thank you both, worked as expected. But druuna both of your commands output are in the same line where will be "user@hostname:~$", it is okay I can cut and paste the output.
Once again thanks a lot druuna and millgates.

Last edited by nitya; 02-20-2013 at 05:43 AM.
 
Old 02-20-2013, 06:36 AM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by nitya View Post
both of your commands output are in the same line where will be "user@hostname:~$"
That's because the first example just replaces all newlines with spaces, including the trailing one. So there's no newline at the end of output and the bash prompt ends up on the same line. The awk example reads the file line by line and outputs them delimited by spaces. Again, no newline at the end of output. My examples work in a similar way, but I use echo to print the result, which automatically appends a newline to whatever it prints (this can be overriden by the -n switch). If you want those trailing \ns to be there, you can print them yourself:

Code:
cat infile | tr "\n" " " && echo
and

Code:
awk 'BEGIN{ ORS=" " }{ print }END{ print "\n"}' infile
or

Code:
awk 'BEGIN{ ORS=" " }{ print }' infile && echo
But it depends on what you want to do with the output. Do you want to use it in a script? Put it in a file?
 
1 members found this post helpful.
Old 02-20-2013, 07:33 AM   #6
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
Adding to druuna post.
Quote:
cat infile | tr "\n" " " | grep -v @
Will work. i have ignored "user@hostname:~$" using grep command. :P
 
2 members found this post helpful.
Old 02-20-2013, 09:58 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Code:
paste -sd' ' file
 
1 members found this post helpful.
Old 02-20-2013, 10:10 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
echo $(<file)
 
2 members found this post helpful.
Old 02-21-2013, 02:58 AM   #9
nitya
LQ Newbie
 
Registered: Feb 2013
Location: India
Distribution: RedHat, Cent, Fedora, Ubuntu
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thank you all for giving lot of ways to achieve this.
Code:
@millgates
foo="$(<infile)"
echo "${foo//$'\n'/ }"

<infile mapfile -t array
echo "${array[@]}"

cat infile | tr "\n" " " && echo

awk 'BEGIN{ ORS=" " }{ print }' /tmp/test && echo

@RaviTezu
cat infile | tr "\n" " " | grep -v @

@grail
paste -sd' ' file

@colucix
echo $(<file)
All above commands worked great. This thread helped me a lot to know about different commands.
Once again thank you all.

Last edited by nitya; 02-21-2013 at 02:59 AM.
 
Old 02-21-2013, 04:51 AM   #10
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
One more way:
Code:
~$ for word in $(cat /tmp/test); do echo -n "$word "; done; echo
 
Old 02-21-2013, 05:11 AM   #11
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by shivaa View Post
One more way:
Code:
~$ for word in $(cat /tmp/test); do echo -n "$word "; done; echo
In this case it will work, but keep in mind that reading files with for like this is generally a bad idea.

Code:
~$ while read word; do echo -n "$word"; done < /tmp/test; echo
might be a bit better.
 
Old 02-21-2013, 05:19 AM   #12
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
Quote:
Originally Posted by millgates;
In this case it will work, but keep in mind that reading files with for like this is generally a bad idea.
Oh yes, you're right.. how can I forget David the H's lession on use while instead of for.

@Everyone:
How can I read a file (data stream, variable) line-by-line (and/or field-by-field)? (Link)
And
Why you don't read lines with "for" (Link)
 
Old 02-21-2013, 05:32 AM   #13
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Code:
xargs echo < file
 
1 members found this post helpful.
Old 02-21-2013, 05:32 AM   #14
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Code:
xargs echo < file
 
1 members found this post helpful.
  


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
[SOLVED] bash to print words/lines vertically nitya Linux - Newbie 6 02-20-2013 06:19 AM
[SOLVED] get two/more specific words on a line and print next few lines Kashif_Bash Programming 11 04-26-2012 12:15 AM
[SOLVED] bash print only found words newbie0101 Linux - Software 5 01-13-2012 03:09 AM
how many lines and words (bash) sharapchi Programming 4 12-15-2006 12:45 PM
Bash - How to make For read lines instead of words in a list ? landuchi Programming 10 02-15-2006 01:36 PM

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

All times are GMT -5. The time now is 07:16 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