forked from kgateway-dev/kgateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit-file-by-delimiter.sh
executable file
·31 lines (26 loc) · 1.08 KB
/
split-file-by-delimiter.sh
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
# turn off glob expansion, since default use-case is splitting by lines containing "***""
set -f
# simple parse of input values
file_in=$1 # in expected use-case, is "content/static/content/gloo-security-scan.docgen"
delimiter=$2 # in expected use-case, is "***"
# split file_in to useful components
filename=$(basename -- "$1")
directory_out=$(dirname $1)
base_file_name=`echo "$filename" | cut -d'.' -f1` # won't work if there are multiple extensions (a.b.js)
file_extension=`echo "$filename" | cut -d'.' -f2` # won't work if there are multiple extensions (a.b.js)
# do the actual by-line splitting
echo "splitting $1 by $2\n"
i=0
cur_out="$directory_out/$base_file_name-$i.$file_extension"
rm -f $cur_out
cat $file_in | while read line
do
if echo "$line" | grep -q "$delimiter"; then
cur_out="$directory_out/$base_file_name-$i.$file_extension"
i=$(($i+1))
rm -f $cur_out
echo "Generating File: $cur_out"
fi
echo $line >> $cur_out
done
echo "\nFinished splitting $file_in into subfiles. Results are all located in $directory_out."