-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplit_skus.sh
64 lines (48 loc) · 2.2 KB
/
split_skus.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
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
#!/bin/bash
# Author: Dino Cajic
# Email: dinocajic@gmail.com
# Year: 2017
# Splits the SKU's into multiple pages
# Gets the number of lines in the $input file
# Divides the total number of lines by the number of lines you want to have per file
# For example, 526 total lines / 50 lines per file = 11 total text files
# Puts the lines from $input file into the generated $output files
input="output/mats_and_skus_per_page_clean.txt"
output="output/skus/sku_"
# Number of lines per file. Will dictate how many files are created in the loop later.
linesperfile=20
# Get number of lines
# Divide the number of lines by $linesperfile (i.e. 50)
# Once you divide the number of lines, store the lines into the files
# Read the number of lines located in mats_and_skus_per_page_clean.txt
numoflines=$(wc -l < "$input")
# We're going to let each process run $linesperfile (i.e. 50) lines of text in the next step, so we divide by $linesperfile (i.e. 50)
# Since numoffiles is an integer, we need to add 1 to accomodate for any additional lines
# i.e. 51 lines would require 2 files
numoffiles=$((numoflines / $linesperfile + 1))
# Initialize the current line of the $input file
currentline=1
# Initialize the file count
i=1
# Loop until $i is greater than the number of files that we're trying to create (i.e. 11)
while [ $i -le $numoffiles ]; do
# Create the name of the file
filename=${output}${i}".txt"
# Initialize $j to 1
j=1
# Loop through the file and place the lines from the input file into the output file
# Loop until $j is greater than $linesperfile (i.e. 50 lines per file)
while [ $j -le $linesperfile ]; do
# Grab the current line of the $input file.
# This line will be stored in the newly created output/skus/$filename file
fileline=$(sed "${currentline}q;d" $input)
# Place the line into the file
echo $fileline >> $filename
# Increment the current line of the $input file by 1
let currentline=$currentline+1
# Increment $j by 1 so that only a certain amount of lines can be placed into the newly created $filename
let j=$j+1
done
# Increment $i so that the file names can be created (i.e. sku_$1.txt)
let i=$i+1
done