0

I have a list of ids as follows, and I want to convert them to a space separated ids (see output below)

INPUT:-

485238
478892
475507
467737
486413
483571
490005

OUTPUT:-

485238 478892 475507 467737 486413 483571 490005
carte blanche
  • 10,796
  • 14
  • 46
  • 65

3 Answers3

4

This might work for you (GNU sed):

sed ':a;$!N;s/\n/ /;ta' file

But really this is a job for paste:

paste -sd\  file 

N.B. there is a space after the backslash and a space before the file

potong
  • 55,640
  • 6
  • 51
  • 83
2

Using awk, you can do this:

awk '{printf "%s ",$0}' file
485238 478892 475507 467737 486413 483571 490005
Jotne
  • 40,548
  • 12
  • 51
  • 55
1
$ more test.txt
485238
478892
475507
467737
486413
483571
490005
$ sed ':a;N;$!ba;s/\n/ /g' test.txt
485238 478892 475507 467737 486413 483571 490005

If you want to send this to a new file:

$ sed ':a;N;$!ba;s/\n/ /g' test.txt > test2.txt
$ more test2.txt 
485238 478892 475507 467737 486413 483571 490005
cianius
  • 2,272
  • 6
  • 28
  • 41