42 lines
1.3 KiB
Bash
Executable File
42 lines
1.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
#This script sanitizes a file in the following manner:
|
|
#it reads a pattern and a destFile
|
|
#then, all lines in destFILE that can be regexp'd to pattern are removed.
|
|
#optionally, a confirm variable is also specified; if so, the script does not prompt the user before overwriting destFILE
|
|
|
|
#this script takes the following input:
|
|
#sanitize.sh pattern destFile [confirm]
|
|
|
|
#make sure input is valid
|
|
[[ "$1" == "" || "$2" == "" ]] && printf "Incorrect input format.\nUse: \t sanitize.sh pattern destFile [confirm]\n" && exit
|
|
[[ ! -f "$2" ]] && printf 'destFile - %s - does not exist!\n' "$2" && exit
|
|
|
|
|
|
PATTERN=$1
|
|
destFile=$2
|
|
saveArray=()
|
|
discardArray=()
|
|
[[ "$3" =~ [yY][eE][sS]|[yY]|1|[tT][rR][uU][eE] ]] && confirm=0 || confirm=1
|
|
|
|
#go through each line, adding to either SAVE or DISCARD as necessary
|
|
while read -r line; do
|
|
[[ ! $line =~ $PATTERN ]] && saveArray+=("$line") || discardArray+=("$line")
|
|
done < "$destFile"
|
|
|
|
#confirm output
|
|
if [[ $confirm -eq 1 ]]; then
|
|
printf "Discarding the following:\n"
|
|
printf '%s\n' "${discardArray[@]}"
|
|
else
|
|
printf 'Discarding %d line(s).\n' "${#discardArray[@]}"
|
|
fi
|
|
|
|
#confirm the user is ok with this
|
|
response="y"
|
|
[[ $confirm -eq 1 ]] && read -r -p "Proceed? (y/N): " response
|
|
if [[ "$response" =~ [yY][eE][sS]|[yY] ]]; then
|
|
echo "Removing unwanted lines..."
|
|
printf '%s\n' "${saveArray[@]}" > "$destFile"
|
|
fi
|