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

  • A+
所属分类:Shell

将某个目录下的文件合并成一行,中间用冒号(:)作为分隔符
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

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: