LoginSignup
27

More than 5 years have passed since last update.

posted at

updated at

linuxでファイルを複数行を一行にする方法

1, trコマンドを利用する

~ koukou.chou.ts$ cat  test
1
2
3
4
5
6
7
8
9
10
~ koukou.chou.ts$ cat test | tr "\n" " ";echo
1 2 3 4 5 6 7 8 9 10 

2, awkコマンドを利用する

~ koukou.chou.ts$ awk '{if(NR%10){ORS=" "}else{ORS="\n"};print;}' test
1 2 3 4 5 6 7 8 9 10

3, perlスクリプトを利用

~ koukou.chou.ts$ perl -pe 's/\n/ /;' test && echo
1 2 3 4 5 6 7 8 9 10 

4, xargを利用

~ koukou.chou.ts$ cat test | xargs
1 2 3 4 5 6 7 8 9 10

5, vim のsedを利用

~ koukou.chou.ts$ vim test 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
~                                                                                                                                                    
~                                                                                                                                                    
~                                                                                                                                                    
                                                                                                                                                  :%s/\n/ /g

6, sedを利用する

~ koukou.chou.ts$ sed 'N;N;N;N;N;N;N;N;N;s/\n/ /g' test
1 2 3 4 5 6 7 8 9 10
~ koukou.chou.ts$ 

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
27