Skip to content

Instantly share code, notes, and snippets.

@KatelynHaworth
Created May 5, 2016 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KatelynHaworth/99a65aaf28f1eb5c61b8fae7fda679ef to your computer and use it in GitHub Desktop.
Save KatelynHaworth/99a65aaf28f1eb5c61b8fae7fda679ef to your computer and use it in GitHub Desktop.
Simple little bash function to search file for a section of text when you only now the start and end of the sections (Used to search postgresql database dumps)
#// Searchs a file for a blob (section)
#// and returns what it finds in a array
#//
#// PARAMS:
#// $1 String - The name of the file to search
#// $2 String - The upper bound (start) of the blob
#// $3 String - The lower bound (end) of the blob
#// $4 Boolean - Defines if the contense of the bounds should be included in the results
findBlobInFile() {
mapfile -t lines < <(cat $1)
upperBound=$2
lowerBound=$3
addLines=false
grabedLines=()
[ -z "${4}" ] && includeBounds=false || includeBounds=$4
for lineIndex in $(seq 0 $[${#lines[@]} - 1]); do
line=${lines[$lineIndex]}
if [[ "${line}" == "${upperBound}" ]] || [[ "${line}" == *"${upperBound}"* ]]; then
addLines=true
$includeBounds && grabedLines+=("$line")
elif [[ "${line}" == "${lowerBound}" ]] || [[ "${line}" == *"${lowerBound}"* ]]; then if $addLines; then
addLines=false
$includeBounds && grabedLines+=("$line")
fi; elif $addLines; then
grabedLines+=("$line")
fi
done
echo "$(declare -p grabedLines)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment