Mailinglist Archive: opensuse (2459 mails)

< Previous Next >
Re: [opensuse] Append output to the same line in a file
  • From: Anders Johansson <ajh@xxxxxxxxxx>
  • Date: Wed, 19 Mar 2008 22:47:45 +0100
  • Message-id: <47E18A01.6090201@xxxxxxxxxx>
Dirk Moolman wrote:
Let me first start with an example:


Testfile:

This is line 1
This is line 2
This is line 3


Script:

for i in `cat testfile`
do
echo $i
done


Output:

This
is
line
1
This
is
line
2
This
is
line
3

First problem - the lines are broken up and not displayed as they were
anymore.


Question:

How can I get the output all on 1 line ? Let's say I want the 3 lines
in the testfile to be all on 1 line, like this:

This is line 1 This is line 2 This is line 3

First off, don't use "cat" for this. Use the bash builtin read, it was
made for reading from files.

To get the three on separate lines without breaking on word boundaries, do

while read a; do
echo $a
done < testfile

To get everything on one single line, do

while read a; do
echo -n $a
done < testfile

You might want to put a blankspace in there

while read a; do
echo -n $a
echo -n " "
done < testfile

Anders
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >
References