write bash code to loop over files and split by regexp
- User
write bash code to loop over files and split by regexp
Assistant
Here's a bash script that loops over files and splits them based on a regular expression:
#!/bin/bash # Usage: ./split_files.sh "pattern" "directory" PATTERN="$1" DIR="${2:-.}" # Default to current directory if not specified for file in "$DIR"/*; do if [ -f "$file" ]; then filename=$(basename "$file") echo "Processing $filename..." # Create output files awk -v pattern="$PATTERN" ' $0 ~ pattern {outfile = FILENAME "." ++count; print > outfile; next} {print > outfile} ' "$file" echo "Split $filename into $(ls "$file".*|wc -l) parts" fi doneWould you like me to explain or break down this code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.