3

I have file:

xxx.lst

with values:

111
222
333

I just need to make one line with:

111 222 333

into a variable or standard output.

4 Answers 4

7

paste is probably the best tool for this job:

$ paste -sd ' ' file
111 222 333
4
  • Awesome, didn't even know this exists, even on AIX :-). Thx. Commented Apr 13, 2016 at 8:15
  • @user165435: It works in any POSIX compliant system. Commented Apr 13, 2016 at 8:26
  • Nice, I looked it up, paste belongs to coreutils package Commented Apr 14, 2016 at 2:19
  • @warl0ck: It's POSIX standard, too. Commented Apr 14, 2016 at 2:23
3

You can use tr to replace line separator with space, e.g

set -f
somevar="$(tr '\n' ' ' < xxx.lst)"
echo $somevar
1
  • works of course Commented Apr 13, 2016 at 8:16
0

Xargs echo and i/o redirection.

xargs echo < xxx.lst

You don't even need the echo.

xargs < xxx.lst

Tested on ksh/aix and bash/GNU.

2
  • Note that xargs choked on unbalance double quotes line, remove balance double quotes line. Commented Apr 13, 2016 at 13:32
  • True! xargs -0 < xxx.lst with GNU xargs will work with any unbalanced quote marks. Commented Apr 13, 2016 at 13:43
0

This also work:

set -f; printf '%s ' $(cat infile )

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.