You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unix脚本开发求助:按文件名模式合并多文本文件

Unix新手文件合并脚本解决方案

Hey there! As someone new to Unix, let's build this file merging script together—we'll make sure it's clear and follows your exact rules.

First, let's recap your requirements to keep us aligned:

合并规则

  • Rule 1: Files where the second part (split by _) is 555A and the third part contains ORANGE (case-insensitive) → merge into AAL_555a_Orange_YYYYMMDD.txt
  • Rule 2: Files where the second part is 555B and the third part contains ORANGE (case-insensitive) → merge into AAL_555b_Orange_YYYYMMDD.txt
  • Rule 3: Files where the second part is 555A and the third part contains MANGO (case-insensitive) → merge into AAL_555a_Mango_YYYYMMDD.txt

完整脚本

#!/bin/bash

# Get current date in YYYYMMDD format (reuse this variable instead of calling date multiple times)
current_date=$(date +"%Y%m%d")

# Define output file names for readability
output_a_orange="AAL_555a_Orange_${current_date}.txt"
output_b_orange="AAL_555b_Orange_${current_date}.txt"
output_a_mango="AAL_555a_Mango_${current_date}.txt"

# Clear output files (start fresh each time the script runs)
> "$output_a_orange"
> "$output_b_orange"
> "$output_a_mango"

# Loop through all files containing ".TXT" in their name (matches your example filenames)
for file in *.TXT*; do
    # Skip if it's a directory (avoid processing folders by accident)
    [[ -f "$file" ]] || continue

    # Split the filename into parts using underscore as separator
    # str1 = first segment (e.g., AAAL), num = second segment (e.g., 555A), fruit_part = third segment (e.g., ORANGE1)
    IFS='_' read -r str1 num fruit_part <<< "$file"

    # Check which category the file belongs to
    if [[ "$num" == "555A" ]]; then
        # Match ORANGE (case-insensitive)
        if [[ "$fruit_part" =~ [Oo][Rr][Aa][Nn][Gg][Ee] ]]; then
            cat "$file" >> "$output_a_orange"
        # Match MANGO (case-insensitive)
        elif [[ "$fruit_part" =~ [Mm][Aa][Nn][Gg][Oo] ]]; then
            cat "$file" >> "$output_a_mango"
        fi
    elif [[ "$num" == "555B" ]]; then
        # Match ORANGE (case-insensitive)
        if [[ "$fruit_part" =~ [Oo][Rr][Aa][Nn][Gg][Ee] ]]; then
            cat "$file" >> "$output_b_orange"
        fi
    fi
done

echo "Merge complete! Check the following files:"
echo "- $output_a_orange"
echo "- $output_b_orange"
echo "- $output_a_mango"

脚本逐行解释

Let's break down each part so you understand exactly what's happening:

  1. Shebang Line: #!/bin/bash tells the system to use the Bash shell to run this script—this is standard for Unix shell scripts.
  2. Date Variable: current_date=$(date +"%Y%m%d") captures today's date in the format you need (e.g., 20240520) and stores it in a variable. This is more efficient than calling date every time we need the date.
  3. Output File Variables: We define variables for each output file to make the script easier to read and modify later (if you ever need to change filenames, you only update one line).
  4. Clear Output Files: The > "$output_file" syntax empties the file if it already exists, so we start with a fresh file each time the script runs (no duplicate content from previous runs).
  5. Loop Through Files: for file in *.TXT* loops through all files in the current directory that have ".TXT" in their name—this matches your example filenames like AAAL_555A_ORANGE1_F190404_D190408.TXT.freshfruits_20190422-115617.
  6. Skip Directories: [[ -f "$file" ]] || continue ensures we only process actual files, not directories that might accidentally match the filename pattern.
  7. Split Filename: IFS='_' read -r str1 num fruit_part <<< "$file" splits the filename into segments using underscores as separators. We capture the first three segments since those are what we need for your rules.
  8. Condition Checks: We use if/elif statements to match your rules:
    • First, we check if the second segment (num) is 555A or 555B
    • We use case-insensitive regex patterns (like [Oo][Rr][Aa][Nn][Gg][Ee]) to match "ORANGE" regardless of uppercase/lowercase—this ensures files like Orange6 are included.
    • cat "$file" >> "$output_file" appends the content of each matching file to the correct output file.
  9. Completion Message: The final echo statements tell you the script is done and point you to the merged files.

使用步骤

  1. Save this script as merge_fruit_files.sh in the directory where your text files are stored.
  2. Make it executable with this command: chmod +x merge_fruit_files.sh
  3. Run it with: ./merge_fruit_files.sh

That's all! This script will handle all your file merging needs based on the rules you provided.

内容的提问来源于stack exchange,提问作者anil busupalli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:37:56