-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.bash
executable file
·149 lines (136 loc) · 4.31 KB
/
merge.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
# Part of the Progressable package
# https://github.com/verseles/progressable
# Function to display help
function show_help {
echo "Usage: $0 [options]"
echo "Options:"
echo " --folder=\"path\" Specifies a folder to merge files (can be used multiple times)"
echo " --folder-recursive=\"path\" Specifies a folder to merge files recursively (can be used multiple times)"
echo " --output=\"file name\" Specifies the output file name (default: output.txt)"
echo " --ignore-extensions=\"extensions\" Specifies file extensions to ignore (comma separated)"
echo " --ignore-folders=\"folders\" Specifies folders to globally ignore (can be used multiple times)"
echo " --ignore-files=\"files\" Specifies files to globally ignore (can be used multiple times)"
echo ""
echo "Examples:"
echo " $0 --folder=/path/folder1 --folder=/path/folder2"
echo " $0 --folder-recursive=/path/folder --output=result.txt"
echo " $0 --folder=/path/folder1 --folder-recursive=/path/folder2 --ignore-extensions=txt,log --ignore-files=file1,file2"
}
# Variables to store parameters
folders=()
recursive_folders=()
output_file="output.txt"
ignore_extensions=""
ignore_folders=()
ignore_files=()
# Process parameters
while [[ $# -gt 0 ]]; do
case "$1" in
--folder=*)
folders+=("${1#*=}")
shift
;;
--folder-recursive=*)
recursive_folders+=("${1#*=}")
shift
;;
--output=*)
output_file="${1#*=}"
shift
;;
--ignore-extensions=*)
ignore_extensions="${1#*=}"
shift
;;
--ignore-folders=*)
ignore_folders+=("${1#*=}")
shift
;;
--ignore-files=*)
ignore_files+=("${1#*=}")
shift
;;
*)
echo "Invalid option: $1"
show_help
exit 1
;;
esac
done
# Clear output file otherwise it will append
truncate -s 0 "$output_file"
# Verify if no parameter was provided
if [[ ${#folders[@]} -eq 0 && ${#recursive_folders[@]} -eq 0 ]]; then
show_help
exit 0
fi
# Function to process a folder
function process_folder {
local folder="$1"
for file in "$folder"/*; do
if [[ -f "$file" ]]; then
local filename="${file##*/}"
if [[ "$file" != "$output_file" && "$folder/$output_file" != "$file" ]]; then
if [[ " ${ignore_files[*]} " != *" $filename "* ]]; then
local extension="${file##*.}"
if [[ "$ignore_extensions" != *"$extension"* ]]; then
ignore_match=false
for ignored_folder in ${ignore_folders[*]}; do
if [[ "${file}/" =~ ${ignored_folder} ]]; then
ignore_match=true
break
fi
done
if [[ ${ignore_match} == false ]]; then
echo "Adding file: $file"
echo "FILE: $file" >> "$output_file"
cat "$file" >> "$output_file"
echo "" >> "$output_file"
fi
fi
fi
fi
fi
done
}
# Function to process a folder recursively
function process_folder_recursive {
local folder="$1"
for file in "$folder"/*; do
if [[ -f "$file" ]]; then
local filename="${file##*/}"
if [[ "$file" != "$output_file" && "$folder/$output_file" != "$file" ]]; then
if [[ " ${ignore_files[*]} " != *" $filename "* ]]; then
local extension="${file##*.}"
if [[ "$ignore_extensions" != *"$extension"* ]]; then
ignore_match=false
for ignored_folder in ${ignore_folders[*]}; do
if [[ "${file}/" =~ ${ignored_folder} ]]; then
ignore_match=true
break
fi
done
if [[ ${ignore_match} == false ]]; then
echo "Adding file recursively: $file"
echo "FILE: $file" >> "$output_file"
cat "$file" >> "$output_file"
echo "" >> "$output_file"
fi
fi
fi
fi
elif [[ -d "$file" ]]; then
process_folder_recursive "$file" # Recursively call the function for subfolders
fi
done
}
# Process specified folders
for folder in "${folders[@]}"; do
process_folder "$folder"
done
# Process specified folders recursively
for folder in "${recursive_folders[@]}"; do
process_folder_recursive "$folder"
done
echo "Output file: $output_file"