convert a list into a single line

convert a list into a single line

Post by Jon Ts » Thu, 05 Jul 2001 22:43:04



Hi

I would like to convert a list, such as:

item1
item2
item3
...
itemN

Into this:

item1 item2 item3 ... itemN
(please note there is a white space between the strings)

I am currently doing the conversion using ^J in vi, but I'm sure there is an
easier and quicker way to do it say in sed/awk/ksh ?

Any ideas?

Cheers.

Jon Tsu

 
 
 

convert a list into a single line

Post by Adrian Bal » Thu, 05 Jul 2001 23:10:01



> I would like to convert a list, such as:

> item1
> item2
> item3
> ...
> itemN

> Into this:

> item1 item2 item3 ... itemN
> (please note there is a white space between the strings)

Bourne shell or compatible:-

while read line ;do echo "$line \c"; done < inputfile > outputfile

Note: depending on your shell you may need 'echo -e' (eg bash).  Also, you
may want to finish with another echo to add a newline, and also there will
be a trailing space which may or may not be important enough to do
something with.

Or:-

awk '{printf "%s ", $0}' < inputfile > outputfile

HTH,
Adrian.

--
Adrian Ball                       \\  Tel: 07050 688565
Major Data Services Ltd            \\  Fax: 07020 965488

http://www.majords.co.uk/

 
 
 

convert a list into a single line

Post by Ian P. Springe » Fri, 06 Jul 2001 02:21:31


Quote:> > I would like to convert a list, such as:

> > item1
> > item2
> > item3
> > ...
> > itemN

> > Into this:

> > item1 item2 item3 ... itemN
> > (please note there is a white space between the strings)

Assuming the list is in a file named 'thefile', easiest way is:

  items=`cat thefile`

Backticks automatically convert any whitespace to single spaces.

To write the data back out to a second file 'somefile':

  echo "$items" >somefile

-Ian

 
 
 

convert a list into a single line

Post by Lee We » Fri, 06 Jul 2001 03:30:22



> Hi

> I would like to convert a list, such as:

> item1
> item2
> item3
> ...
> itemN

> Into this:

> item1 item2 item3 ... itemN
> (please note there is a white space between the strings)

> I am currently doing the conversion using ^J in vi, but I'm sure there is an
> easier and quicker way to do it say in sed/awk/ksh ?

$ cat > list
item1
item2
item3
item4
item5
$ tr '[\n]' '[ ]'  < list > list2
$ more list2
item1 item2 item3 item4 item5
$

Lee.

 
 
 

convert a list into a single line

Post by Chris F.A. Johnso » Fri, 06 Jul 2001 07:08:59



> Hi

> I would like to convert a list, such as:

> item1
> item2
> item3
> ...
> itemN

> Into this:

> item1 item2 item3 ... itemN
> (please note there is a white space between the strings)

> I am currently doing the conversion using ^J in vi, but I'm sure there is an
> easier and quicker way to do it say in sed/awk/ksh ?

What is the source of the list? A file? A variable? Command output?

Whichever it is, there are many ways of doing it (not to mention UUOC).

A file:
        echo `cat FILE`
        tr "\n" " " < FILE

A variable:
        echo $VAR

Command output:
        echo `command`

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

convert a list into a single line

Post by gary » Fri, 06 Jul 2001 08:23:01


An alternative would be the paste command with the -s option (to paste all
subsequent lines into one) and the -d option to specify the space as the
delimiter.

A couple of examples:

$ paste -sd' ' filename

$ command_that_generates_list | paste -sd' ' -
(the - at the end of the above example tells paste to take its input from standard
input instead of a file)

Regards,
Gary


>Hi

>I would like to convert a list, such as:

>item1
>item2
>item3
>...
>itemN

>Into this:

>item1 item2 item3 ... itemN
>(please note there is a white space between the strings)

>I am currently doing the conversion using ^J in vi, but I'm sure there is an
>easier and quicker way to do it say in sed/awk/ksh ?

>Any ideas?

>Cheers.

>Jon Tsu

 
 
 

convert a list into a single line

Post by Juergen P. Mei » Fri, 06 Jul 2001 14:49:18



Quote:>> > I would like to convert a list, such as:

>> > item1
>> > item2
>> > item3
>> > ...
>> > itemN

>> > Into this:

>> > item1 item2 item3 ... itemN
>> > (please note there is a white space between the strings)

>Assuming the list is in a file named 'thefile', easiest way is:

>  items=`cat thefile`

>Backticks automatically convert any whitespace to single spaces.

>To write the data back out to a second file 'somefile':

>  echo "$items" >somefile

This works fine as long as the file is not larger than the limit
on the comandline length.

Some versions of tr -d "
" infile >outfile

can do it nicely (the line break is intended!)
(tested with gnu tr and solaris' tr)

juergen
--

 
 
 

convert a list into a single line

Post by * Tong » Sun, 08 Jul 2001 06:27:54



> Hi

> I would like to convert a list, such as:

> item1
> item2
> item3
> ...
> itemN

> Into this:

> item1 item2 item3 ... itemN

If it is used for shell programming, another good ways is to use
xargs:

cat listfile | xargs echo

and echo can be any command you want.

--
Tong (remove underscore(s) to reply)
  *niX Power Tools Project: http://xpt.sourceforge.net/
  - All free contribution & collection

 
 
 

convert a list into a single line

Post by nos.. » Sun, 08 Jul 2001 22:58:01



> I would like to convert a list, such as:

> item1
> item2
> item3
> ...
> itemN

> Into this:

> item1 item2 item3 ... itemN

while read item;do
  list="$list $item"
done < datafile
 
 
 

convert a list into a single line

Post by Ted J. Loeffelhol » Tue, 31 Jul 2001 20:57:54



> Hi

> I would like to convert a list, such as:

> item1
> item2
> item3
> ...
> itemN

> Into this:

> item1 item2 item3 ... itemN
> (please note there is a white space between the strings)

> I am currently doing the conversion using ^J in vi, but I'm sure there is an
> easier and quicker way to do it say in sed/awk/ksh ?

> Any ideas?

> Cheers.

> Jon Tsu

You could try using ....

awk '{printf $0 " "}' filename

I am sure there is probably a way to shorten it up or even to use sed to
substitute a space or tab for a return but this is the first thing that
came to mind.

--
Cheers,
Ted J. Loeffelholz
Senior Design Engineer
Caterpillar Inc

 
 
 

convert a list into a single line

Post by Alexis Huxl » Wed, 01 Aug 2001 00:03:51


Quote:> > I would like to convert a list, such as:

> > item1
> > item2
> > item3
> > ...
> > itemN

> > Into this:

> > item1 item2 item3 ... itemN

If the list is going to be reasonably short then try piping it into
'xargs echo', which will also do with multi/leading/trailing blanks
exactly what you'd like it to do.

Alexis

 
 
 

convert a list into a single line

Post by Roger Denho » Wed, 01 Aug 2001 20:18:43




>> > I would like to convert a list, such as:

>> > item1
>> > item2
>> > item3
>> > ...
>> > itemN

>> > Into this:

>> > item1 item2 item3 ... itemN

tr   '\n' ' ' < infile > outfile
print >> outfile

cheers
Roger D

 
 
 

convert a list into a single line

Post by John Savag » Fri, 03 Aug 2001 05:54:48




>> I would like to convert a list, such as:

>> item1
>> item2
>> item3
>> ...
>> itemN

>> Into this:

>> item1 item2 item3 ... itemN

>If it is used for shell programming, another good ways is to use
>xargs:

>cat listfile | xargs echo

>and echo can be any command you want.

If this vertical list of words is stored in a file, then you can
assign them to a variable using: wordlist=`xargs <file`

If this vertical list of words is stored in a variable, then you
can assign them to a variable using: wordlist=`echo $var`

Verify the outcome using: echo "$wordlist"
--
John Savage  (one month late in responding, but bash probably hasn't changed)