Dynamically splitting output into files while processing

From Christiaan008 Wiki
Revision as of 22:35, 8 November 2015 by Christiaan008 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#!/bin/bash

#The purpose of this script is to dynamically split the output of generated entries into different files 
#Set the variable values before the while loop so that there is some input
start="0"
amount="5" #The amount to use to split
begin=$(echo $start) #The first time this will be the start value
end=$(expr $amount - 1) #The first time this will be the amount minus 1 value

#This loop will continue until the variable $start will hit 10
while [[ $start -lt 20 ]]
do
        #Check if the expected file exists (only necessary for the first run)
        if [[ -f  "numbers"$begin"_"$end".txt" ]];
        then
                #Count the amount of lines in the current file
                lines=$(wc -l < "numbers"$begin"_"$end".txt")
        else
                lines="0"
        fi

        #If the result of the amount of lines in the current file is less then the number that is set add the entry to the current file
        if [[ $lines -lt $amount ]]
        then
                echo $start >> "numbers"$begin"_"$end".txt"
        #Else the amount of lines in the current file is more then the amount that is set in the value $amount
        else
                #Update the variables $begin and $end by adding the value of variable $amount
                begin=$(expr $begin + $amount)
                end=$(expr $end + $amount)
                #Add current entry to new file
                echo $start >> "numbers"$begin"_"$end".txt"
        fi

        #Update the variable $start by 1 otherwise the loop will run indefinitely
        start=$(expr $start + 1)
done