可以使用以下Bash脚本将多个.csv文件合并成一个:
#!/bin/bash
# Set the input directory containing CSV files
input_dir=/path/to/csv/files/
# Set the output file name and location
output_file=/path/to/output/merged.csv
# Write the header row to the output file
# Assumes all files have the same header
cat ${input_dir}/*.csv | head -n 1 > ${output_file}
# Append all files to the output file except the header
# Skips the first line from all files (header row)
for file in ${input_dir}/*.csv; do
sed 1d $file >> ${output_file}
done
该脚本假定所有.csv文件都位于同一个目录中,并将它们合并成一个名为merged.csv的输出文件。它还假定所有文件都具有相同的标题行。如果您不确定文件是否有相同的标题行,或者需要处理其他标题行的情况,请更改第5行的head -n 1
和第10行的sed 1d
。
使用该脚本将多个.csv文件合并成一个:
chmod +x combine_csv.sh
赋予该文件可执行权限。./combine_csv.sh
运行脚本。