脚本如何多文件内容合并

wen 实用脚本 27

本文目录导读:

脚本如何多文件内容合并

  1. 使用 Shell 命令(Linux/Mac)
  2. Python 脚本
  3. PowerShell(Windows)
  4. Node.js 脚本
  5. 处理特殊格式
  6. 使用 find 和 xargs 组合
  7. 实用建议

使用 Shell 命令(Linux/Mac)

合并文本文件

# 合并当前目录所有txt文件
cat *.txt > merged.txt
# 按特定顺序合并
cat file1.txt file2.txt file3.txt > merged.txt
# 递归合并子目录文件
find . -name "*.txt" -exec cat {} \; > merged.txt

添加文件名分隔符

for file in *.txt; do
    echo "=== $file ===" >> merged.txt
    cat "$file" >> merged.txt
    echo "" >> merged.txt
done

Python 脚本

简单合并

import glob
import os
def merge_files(pattern, output_file):
    with open(output_file, 'w', encoding='utf-8') as outfile:
        for filename in glob.glob(pattern):
            with open(filename, 'r', encoding='utf-8') as infile:
                outfile.write(f"=== {filename} ===\n")
                outfile.write(infile.read())
                outfile.write("\n\n")
# 使用示例
merge_files("*.txt", "merged_output.txt")

带路径处理的合并

import os
def merge_with_paths(input_dir, output_file, extensions=['.txt', '.md']):
    with open(output_file, 'w', encoding='utf-8') as outfile:
        for root, dirs, files in os.walk(input_dir):
            for file in files:
                if any(file.endswith(ext) for ext in extensions):
                    filepath = os.path.join(root, file)
                    outfile.write(f"--- {filepath} ---\n")
                    with open(filepath, 'r', encoding='utf-8') as infile:
                        outfile.write(infile.read())
                    outfile.write("\n\n")
# 使用
merge_with_paths("./documents", "merged.txt")

PowerShell(Windows)

# 合并所有文本文件
Get-Content *.txt | Out-File merged.txt
# 带文件名标记
Get-ChildItem *.txt | ForEach-Object {
    "=== $($_.Name) ===" | Out-File merged.txt -Append
    Get-Content $_.FullName | Out-File merged.txt -Append
    "`n" | Out-File merged.txt -Append
}

Node.js 脚本

const fs = require('fs');
const path = require('path');
const glob = require('glob'); // npm install glob
async function mergeFiles(pattern, outputFile) {
    const files = glob.sync(pattern);
    const writeStream = fs.createWriteStream(outputFile);
    for (const file of files) {
        writeStream.write(`=== ${file} ===\n`);
        const content = fs.readFileSync(file, 'utf-8');
        writeStream.write(content);
        writeStream.write('\n\n');
    }
    writeStream.end();
    console.log(`Merged ${files.length} files to ${outputFile}`);
}
// 使用
mergeFiles('./**/*.txt', 'merged.txt');

处理特殊格式

合并 JSON 文件

import json
import glob
def merge_json_files(pattern, output_file):
    merged_data = {}
    for filename in glob.glob(pattern):
        with open(filename, 'r') as f:
            data = json.load(f)
            merged_data[filename] = data
    with open(output_file, 'w') as f:
        json.dump(merged_data, f, indent=2)

合并 CSV 文件(保留表头)

import pandas as pd
import glob
def merge_csv_files(pattern, output_file):
    all_files = glob.glob(pattern)
    dfs = []
    for file in all_files:
        df = pd.read_csv(file)
        dfs.append(df)
    merged_df = pd.concat(dfs, ignore_index=True)
    merged_df.to_csv(output_file, index=False)

使用 find 和 xargs 组合

# Linux/Mac
find . -type f -name "*.txt" -exec echo "--- {} ---" \; -exec cat {} \; > merged.txt
# 或使用 xargs
find . -name "*.txt" -print0 | xargs -0 -I {} sh -c 'echo "=== {} ===" && cat {}' > merged.txt

实用建议

  1. 处理大文件:使用流式处理避免内存溢出
  2. 编码问题:注意统一文件编码(UTF-8 最常用)
  3. 文件排序:需要特定顺序时,先排序再合并
  4. 错误处理:添加 try-catch 处理无法读取的文件
  5. 断点续传:处理大量文件时考虑断点功能

选择哪种方法取决于你的操作系统、使用场景和个人熟悉程度。

抱歉,评论功能暂时关闭!