1

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

1 Answer 1

2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .