Created
May 5, 2016 05:41
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#// 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