大网数据广告活动
服务器运维问答

Split a file with headers

· 52CE 我爱测

I can split a large file into 3 or 4 parts. But is there any way to repeat the headers of csv file? # split -b 10G user_logs.csv This will create xaa, xab and xac. The original headers are now part of xaa. How do I copy this line…

问题原文

原帖链接 · shantanuo · CC BY-SA 4.0

I can split a large file into 3 or 4 parts. But is there any way to repeat the headers of csv file?

# split -b 10G user_logs.csv

This will create xaa, xab and xac. The original headers are now part of xaa. How do I copy this line to xab and xac?

# head user_logs.csv
msno,date,num_25,num_50,num_75,num_985,num_100,num_unq,total_secs
rxIP2f2aN0rYNp+toI0Obt/N/FYQX8hcO1fTmmy2h34=,20150513,0,0,0,0,1,1,280.3350
rxIP2f2aN0rYNp+toI0Obt/N/FYQX8hcO1fTmmy2h34=,20150709,9,1,0,0,7,11,1658.9480

Will I have to repeat these 3 lines for each file?

head -1 user_logs.csv > xab_temp
cat xab >> xab_temp
mv xab_temp xab

已采纳解答

原帖链接 · Mat · CC BY-SA 4.0

You could try with the --filter. Something like the following:

split -b 10G \
 --filter='if [ ! "$FILE" = "xaa" ]; then echo "$header" > "$FILE";fi;cat >> "$FILE"' file

Example:

$ cat file
this is the header
1
2
3
4
5
6
7
8
9
10
$ export header=$(head -n 1 file)
$ split -l 3 --filter='if [ ! "$FILE" = "xaa" ] ; then echo "$header" > "$FILE" ; fi ; cat >> "$FILE"' file 
$ for x in x* ; do echo "== $x" ; cat $x ; done
== xaa
this is the header
1
2
== xab
this is the header
3
4
5
== xac
this is the header
6
7
8
== xad
this is the header
9
10

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-09-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。