(总结)Linux下多行合并成一行,中间加分隔符

将某个目录下的文件合并成一行,中间用冒号(:)作为分隔符
1、paste

ls lib | sed 's@^@lib/@g' | paste -s -d ":"

2、 tr

ls lib | sed 's@^@lib/@g' | tr "\n" ":" | sed 's@:$@@'

3、xargs

ls lib | sed 's@^@lib/@g' | xargs | sed 's/ /:/g'

4、awk

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

5、perl

ls lib | sed 's@^@lib/@g' | perl -pe 's/\n/:/;' | sed 's@:$@@'

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

Mon Apr 15 11:26:24 CST 2013


【AD】美国洛杉矶CN2 VPS/香港CN2 VPS/日本CN2 VPS推荐,延迟低、稳定性高、免费备份_搬瓦工vps

【AD】RackNerd 推出的 KVM VPS 特价优惠,在纽约、西雅图、圣何塞和阿什本每年仅需 12.88 美元!