LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash to print words/lines horizontally (https://www.linuxquestions.org/questions/linux-newbie-8/bash-to-print-words-lines-horizontally-4175450926/)

nitya 02-20-2013 04:45 AM

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.

druuna 02-20-2013 04:54 AM

Using tr:
Code:

cat infile | tr "\n" " "
Using awk:
Code:

awk 'BEGIN{ ORS=" " }{ print }' infile
There are probably other ways as well.

millgates 02-20-2013 05:03 AM

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.

nitya 02-20-2013 05:37 AM

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.

millgates 02-20-2013 06:36 AM

Quote:

Originally Posted by nitya (Post 4895854)
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?

RaviTezu 02-20-2013 07:33 AM

Adding to druuna post.
Quote:

cat infile | tr "\n" " " | grep -v @
Will work. i have ignored "user@hostname:~$" using grep command. :P

grail 02-20-2013 09:58 AM

Code:

paste -sd' ' file

colucix 02-20-2013 10:10 AM

Code:

echo $(<file)

nitya 02-21-2013 02:58 AM

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.

shivaa 02-21-2013 04:51 AM

One more way: :)
Code:

~$ for word in $(cat /tmp/test); do echo -n "$word "; done; echo

millgates 02-21-2013 05:11 AM

Quote:

Originally Posted by shivaa (Post 4896567)
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.

shivaa 02-21-2013 05:19 AM

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)

whizje 02-21-2013 05:32 AM

Code:

xargs echo < file


All times are GMT -5. The time now is 06:47 PM.