天天看點

linux添加行分隔符,Linux下多行合并成一行,中間加分隔符

将某個目錄下的檔案合并成一行,中間用冒号(:)作為分隔符

1、paste

ls lib | sed '[email protected]^@lib/@g' | paste -s -d ":"

2、 tr

ls lib | sed '[email protected]^@lib/@g' | tr "\n" ":" | sed '[email protected]:[email protected]@'

3、xargs

ls lib | sed '[email protected]^@lib/@g' | xargs | sed 's/ /:/g'

4、awk

ls lib | sed '[email protected]^@lib/@g' | awk "{if(NR%`ls lib | sed '[email protected]^@lib/@g' | wc -l`){ORS=\":\"}else{ORS=\"\n\"};print;}"

5、perl

ls lib | sed '[email protected]^@lib/@g' | perl -pe 's/\n/:/;' | sed '[email protected]:[email protected]@'

6、python

#!/usr/bin/env python

#coding:utf8

import os

def dirList(dir):

filelist = os.listdir(dir)

allfile=[]

for filename in filelist:

allfile.append(dir+'/'+filename)

return allfile

allfile=dirList('lib')

pathjar=':'.join(allfile)

print pathjar

開心洋蔥 , 版權所有丨如未注明 , 均為原創丨未經授權請勿修改 , 轉載請注明Linux下多行合并成一行,中間加分隔符!