Files
eoy-pdf-sortandmerge/Find_Missing_Addresses.sh
T

77 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
#Get the folder with the pdf documents in it.
while :
do
echo -e "What is the directory with the pdfs?"
read PDFDIR
#PDFDIR="/home/btmccrea/Downloads/pdfs"
if [ -d $PDFDIR ]
then
break
else
echo -e "Directory not found."
fi
done
#Get the folder for the output file.
while :
do
echo -e "What is the output directory for the pdf's with missing addresses?"
read OUTPUTFOLDER
#OUTPUTFOLDER="/home/btmccrea/Downloads/output"
if [ -d $OUTPUTFOLDER ]
then
break
else
echo -e "Directory not found."
fi
done
#Create the csv and headers for the Addresses. It is tab separated.
mkdir $OUTPUTFOLDER/Non_Blank
touch $OUTPUTFOLDER/Non_Blank/Addresses.csv
echo -e "File,User,Street,City,State,Zip" > $OUTPUTFOLDER/Non_Blank/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 -e "Copying file to 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/Non_Blank/Addresses.csv
cp "$f" $OUTPUTFOLDER/Non_Blank/
echo -e "Copying file to Non_Blank"
fi
done
exit