echo "Welcome! It must be the start of a new year! In order to get started with sorting and merging the PDF documents I need to know a little more information." echo echo read -p 'Where is the directory that has the PDF statments with address? ' PDFDIR #read PDFDIR #Get the folder for the output file. echo read -p 'What output directory do you want for the sorted PDFs? ' OUTPUTFOLDER #read OUTPUTFOLDER echo echo "The PDFs statments are in $PDFDIR?" echo "The output folder will be $OUTPUTFOLDER." echo read -p "Press Enter to continue" $OUTPUTFOLDER/Addresses/Addresses.csv #Create csv with all the files with missing addresses mkdir $OUTPUTFOLDER/Missing_Addresses touch $OUTPUTFOLDER/Missing_Addresses/Missing_Addresses.csv echo -e "File\tUser" > $OUTPUTFOLDER/Missing_Addresses/Missing_Addresses.csv cd $PDFDIR #FOLDER=$(ls | grep *.pdf) #echo $FOLDER #*.pdf represensts All files in the current directory with a pdf extention. for f in $PDFDIR/* do echo "Processing file $f." #pdftotext converst the file to text and places it in the file name temp pdftotext "$f" - > TEMP #assigns the lines with relevant values to their variable USER=$(sed -n '12p' TEMP) ADDRESS1=$(sed -n '13p' TEMP) ADDRESS2=$(sed -n '14p' TEMP) echo -e "User is $USER" echo -e "Address is $ADDRESS1 $ADDRESS2" if [ -z "$ADDRESS1" ] then #Move the file with the Missing addresses to the Missing Address folder and make an note in its csv. echo "Copying file to the folder called Missing_Addresses" cp "$f" "$OUTPUTFOLDER"/Missing_Addresses/ echo -e "$f\t$USER" >> "$OUTPUTFOLDER"/Missing_Addresses/Missing_Addresses.csv #Adds the row of variables to the csv. >> appends to the file. \t represents tabs else # Split Addresses CITY=$(echo $ADDRESS2 | cut -d, -f1) ADDRESS2=$(echo $ADDRESS2 | sed -e "s/$CITY, //") STATE=$(echo $ADDRESS2 | cut -d" " -f1) ZIP=$(echo $ADDRESS2 | cut -d" " -f2) echo -e "$f,$USER,$ADDRESS1,$CITY,$STATE,$ZIP" >> $OUTPUTFOLDER/Addresses/Addresses.csv cp "$f" $OUTPUTFOLDER/Addresses/ echo "Copying file to the folder called Addresses" fi done echo "All done sorting!" You can find all the Files in $OUTPUTFOLDER!" echo echo read -r -p "Do you want to continue? [Y/n]" response response=${response,,} # tolower if [[ $response =~ ^(y| ) ]] || [[ -z $response ]]; then cd $PDFDIR for FILENAME in $PDFDIR/* do echo $FILENAME #The next Line assigns the number of pages to the value LOCATION to be used for were the file goes LOCATION=$(pdfinfo "$FILENAME" | grep Pages: | awk '{print $2}') #Test if the file exists and if it does move it to Temp.pdf to be merged back to its original location. if test -f $OUTPUTFOLDER/Statement-Pages-$LOCATION.pdf then mv $OUTPUTFOLDER/Statement-Pages-$LOCATION.pdf $OUTPUTFOLDER/temp.pdf pdfunite "$FILENAME" $OUTPUTFOLDER/temp.pdf $OUTPUTFOLDER/Statement-Pages-$LOCATION.pdf else #IF the file doesn't exist create the file with the merge. pdfunite "$FILENAME" $OUTPUTFOLDER/Statement-Pages-$LOCATION.pdf fi done echo echo echo "All done, you can find the PDF statments at $OUTPUTFOLDER." echo echo exit