I tried both options I suggested in my earlier post on my Linux system. Both the xyz* files and the Part* files had .csv extensions. I cannot see that macOS wouldn't run this script correctly. The only difference between my script and yours is that I used a path to the files as I didn't want to risk accidentally creating or deleting files in an unexpected location. Here is my script for reference which works on Linux:
#!/bin/bash
FILENAME=split.csv
HDR="$(head -1 $FILENAME)"
#split -l 3 --additional-suffix=".csv" $FILENAME xyz
split -l 3 $FILENAME xyz
for i in xyz*
do
printf "Renaming $i to $i.csv\n"
mv ./"$i" ./"$i.csv"
done
n=1
for f in xyz*
do
if [ $n -gt 1 ]; then
echo "$HDR" > ./Part"$n".csv
fi
cat $f >> ./Part"$n".csv
rm -i "$f"
((n++))
done
Here is what happens when I run the script on Linux (I declined to delete the temporary files):
Files in folder before running sript....
split.csv
split-script.sh
****************************************
Running script.....
Renaming xyzaa to xyzaa.csv
Renaming xyzab to xyzab.csv
****************************************
Files in folder after running script....
Part1.csv
Part2.csv
split.csv
split-script.sh
xyzaa.csv
xyzab.csv
Here is the contents of the original "split.csv" file:
This is the header line.....
This is line# 1
This is line# 2
This is line# 3
This is line# 4
This is line# 5
Here is the contents of the "Part2.csv" file:
This is the header line.....
This is line# 3
This is line# 4
This is line# 5