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
_) is555Aand the third part containsORANGE(case-insensitive) → merge intoAAL_555a_Orange_YYYYMMDD.txt - Rule 2: Files where the second part is
555Band the third part containsORANGE(case-insensitive) → merge intoAAL_555b_Orange_YYYYMMDD.txt - Rule 3: Files where the second part is
555Aand the third part containsMANGO(case-insensitive) → merge intoAAL_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:
- Shebang Line:
#!/bin/bashtells the system to use the Bash shell to run this script—this is standard for Unix shell scripts. - 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 callingdateevery time we need the date. - 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).
- 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). - 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 likeAAAL_555A_ORANGE1_F190404_D190408.TXT.freshfruits_20190422-115617. - Skip Directories:
[[ -f "$file" ]] || continueensures we only process actual files, not directories that might accidentally match the filename pattern. - 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. - Condition Checks: We use
if/elifstatements to match your rules:- First, we check if the second segment (
num) is555Aor555B - We use case-insensitive regex patterns (like
[Oo][Rr][Aa][Nn][Gg][Ee]) to match "ORANGE" regardless of uppercase/lowercase—this ensures files likeOrange6are included. cat "$file" >> "$output_file"appends the content of each matching file to the correct output file.
- First, we check if the second segment (
- Completion Message: The final
echostatements tell you the script is done and point you to the merged files.
使用步骤
- Save this script as
merge_fruit_files.shin the directory where your text files are stored. - Make it executable with this command:
chmod +x merge_fruit_files.sh - 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
相关产品推荐
相关产品推荐

