如何编写Bash脚本批量处理多目录文件提取指定行片段
Batch Extract Specific Portion of Line 80 from Multiple Files
Let's fix your batch processing issue and adjust that sed command (I notice there's a small typo in your original command—80,24p won't work because the start line number is higher than the end). Here are two practical Bash script solutions depending on how your target files are organized:
Solution 1: Process Specific Directories
If you know exactly which directories contain your target files (named A in your example), use this script to loop through them explicitly:
#!/bin/bash # Set your output file name OUTPUT_FILE="prin" # Clear the output file first (remove this line if you want to append to existing content) > "$OUTPUT_FILE" # List all directories you want to process (replace with your actual directory paths) TARGET_DIRS=("./dir1" "./dir2" "./docs/subdir") for dir in "${TARGET_DIRS[@]}"; do # Check if the directory exists if [ ! -d "$dir" ]; then echo "⚠️ Directory $dir doesn't exist, skipping..." >&2 continue fi FILE_PATH="$dir/A" # Check if the target file exists and has at least 80 lines if [ -f "$FILE_PATH" ] && [ $(wc -l < "$FILE_PATH") -ge 80 ]; then # Extract line 80, then get your desired portion # Example 1: Get everything from character 24 onwards sed -n '80s/^.\{23\}//p' "$FILE_PATH" >> "$OUTPUT_FILE" # Example 2: Get the first 24 characters of line 80 # sed -n '80s/^\(.\{24\}\).*/\1/p' "$FILE_PATH" >> "$OUTPUT_FILE" # Example 3: Use cut instead of sed for simpler range selection # sed -n '80p' "$FILE_PATH" | cut -c24- >> "$OUTPUT_FILE" else echo "⚠️ $FILE_PATH is missing or has fewer than 80 lines, skipping..." >&2 fi done echo "✅ Processing finished! Results saved to $OUTPUT_FILE"
Solution 2: Recursively Find and Process Files
If your target files are scattered across multiple subdirectories and you need to find them automatically, use find to locate all matching files:
#!/bin/bash OUTPUT_FILE="prin" > "$OUTPUT_FILE" # Recursively find all files named "A" (adjust the pattern if your files have different names) find . -type f -name "A" | while read -r FILE_PATH; do if [ $(wc -l < "$FILE_PATH") -ge 80 ]; then # Adjust the substring extraction command to match your needs sed -n '80s/^.\{23\}//p' "$FILE_PATH" >> "$OUTPUT_FILE" else echo "⚠️ $FILE_PATH has fewer than 80 lines, skipping..." >&2 fi done echo "✅ Processing finished! Results saved to $OUTPUT_FILE"
How to Use:
- Copy the script into a file (e.g.,
extract_line80.sh) - Adjust the directory list or file name pattern to match your setup
- Modify the substring extraction command (pick the example that fits your needs, or tweak it further)
- Make the script executable:
chmod +x extract_line80.sh - Run it:
./extract_line80.sh
Key Notes:
- The original
sed -n '80,24p; 24q'command was invalid because you can't specify a range where the start line is higher than the end. We replaced it with commands that target line 80 directly and extract the specific portion you need. - The
wc -l < "$FILE_PATH"check ensures we don't try to extract line 80 from files that are too short, avoiding errors. - Use
>&2for warning messages so they don't get mixed into your output file.
内容的提问来源于stack exchange,提问作者narasimhan v
相关产品推荐
相关产品推荐

