Linux下把文件多行合并成一行的多种方法
发布时间:2016-12-27 来源:查字典编辑
摘要:将某个目录下的文件合并成一行,中间用冒号(:)作为分隔符1、pastelslib|sed's@^@lib/@g'|paste-s-d":"2...
将某个目录下的文件合并成一行,中间用冒号(:)作为分隔符
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