[英]Create a CSV from Text File useing Linux Console
I have a text file that looks like this: 我有一个看起来像这样的文本文件:
line1
line2
line3
line4
line5
"" "" keep going for a long time
I'm trying to come up with a script that would give me: 我正在尝试提出一个可以给我的脚本:
line1,line2,line3,line4,line5
line6,line7,line8,line9,line10
So comma separate them all and add a newline every 5. Any ideas? 因此,用逗号将它们全部分开,并每隔5个添加换行符。有什么想法吗?
paste -d, - - - - - < filename
A bit rough but workable w/out resorting to the, perhaps more pleasing, perl solution. 略带粗糙但可行的方法,而不是求助于可能更令人愉悦的perl解决方案。 The -n 5 arg to xargs makes it just send 5 arguments to the shell script, which we print.
xargs的-n 5 arg使其仅向我们打印的shell脚本发送5个参数。
$ cat echo.sh
echo $1,$2,$3,$4,$5
$ $ cat file.txt
a
b
c
d
e
1
2
3
4
5
$ cat file | xargs -n 5 ./echo.sh
a,b,c,d,e
1,2,3,4,5
$
cat foo.txt | xargs -L 5 | tr ' ' ','
这里的好处是,您还可以将“ 5”参数修改为任意值,并且脚本可以按预期工作。
如果您有perl,请尝试以下操作:
perl -ane '++$i; chomp; $line.=$_; if($i==5) {print "$line\n"; $line=""; $i=0;} else {$line.=","} ' <infile >outfile
Python is installed by default on every linux distro these days. 这些天默认情况下,Python已安装在每个Linux发行版上。
I'd suggest the following python script: 我建议使用以下python脚本:
#!/usr/bin/env python
import argparse, csv
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='convert text to csv', version='%(prog)s 1.0')
parser.add_argument('infile', nargs='+', type=str, help='list of input files')
parser.add_argument('--out', type=str, default='temp.csv', help='name of output file')
args = parser.parse_args()
writer = csv.DictWriter(open(args.out, "wb"), ["field 1","field 2","field 3","field 4","field 5"], dialect='excel')
# write the header at the top of the file
writer.writeheader()
row = []
for fname in args.infile:
with open(fname) as df:
for line in df.readlines():
row.append(line.strip('\n'))
if len(row) = 5:
writer.writerow(row)
row = []
del writer
You should be able to copy the code into a file and run it right off the command line. 您应该能够将代码复制到文件中,然后直接在命令行中运行它。 For instance:
text2csv.py yourinput.txt
if, of course, you called the file text2csv.py
. 例如:
text2csv.py yourinput.txt
如果,当然,你叫文件text2csv.py
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.