mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-13 07:23:47 -05:00
Add new function and improve comments
This commit is contained in:
@@ -275,9 +275,9 @@ _columnizeOutput_() {
|
||||
# NOTE:
|
||||
# Long text or ANSI colors in the first column may create display issues
|
||||
# USAGE:
|
||||
# _columnizeOutput_ 0 30 10 "Key" "Long value text"
|
||||
# _columnizeOutput_ 0 30 "Key" "Long value text"
|
||||
|
||||
[[ $# -lt 5 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||
[[ $# -lt 4 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||
|
||||
local _leftIndent=$1
|
||||
local _leftColumn=$2
|
||||
|
||||
@@ -8,7 +8,7 @@ _dedupeArray_() {
|
||||
# OUTS:
|
||||
# stdout: Prints de-duped elements
|
||||
# USAGE:
|
||||
# _removeDups_ "${array[@]}"
|
||||
# mapfile -t newarray < <(_dedupeArray_ "${array[@]}")
|
||||
# NOTE:
|
||||
# List order may not stay the same
|
||||
# CREDIT:
|
||||
|
||||
@@ -58,7 +58,7 @@ _printArray_() {
|
||||
|
||||
[[ ${VERBOSE:-} != true ]] && return 0
|
||||
|
||||
debug "Printing contents of \${${_arrayName}[@]}" "${_lineNumber}"
|
||||
debug "Contents of \${${_arrayName}[@]}" "${_lineNumber}"
|
||||
|
||||
for _k in "${!_arr[@]}"; do
|
||||
debug "${_k} = ${_arr[${_k}]}"
|
||||
|
||||
@@ -671,6 +671,84 @@ _parseYAML_() {
|
||||
}' | sed 's/_=/+=/g' | sed 's/[[:space:]]*#.*"/"/g'
|
||||
}
|
||||
|
||||
_printFileBetween_() (
|
||||
# DESC:
|
||||
# Prints text of a file between two regex patterns
|
||||
# ARGS:
|
||||
# $1 (Required): Starting regex pattern
|
||||
# $2 (Required): Ending regex pattern
|
||||
# $3 (Required): Input string
|
||||
# OPTIONS:
|
||||
# -i (Optional) - Case-insensitive regex
|
||||
# -r (Optional) - Remove first and last lines (ie - the lines which matched the patterns)
|
||||
# -g (Optional) - Greedy regex (Defaults to non-greedy)
|
||||
# OUTS:
|
||||
# 0: Success
|
||||
# 1: Failure
|
||||
# stdout: Prints text between two regex patterns
|
||||
# USAGE:
|
||||
# _printFileBetween_ "^pattern1$" "^pattern2$" "String or variable containing a string"
|
||||
|
||||
[[ $# -lt 3 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||
|
||||
local _removeLines=false
|
||||
local _greedy=false
|
||||
local _caseInsensitive=false
|
||||
local opt
|
||||
local OPTIND=1
|
||||
while getopts ":iIrRgG" opt; do
|
||||
case ${opt} in
|
||||
i | I) _caseInsensitive=true ;;
|
||||
r | R) _removeLines=true ;;
|
||||
g | G) _greedy=true ;;
|
||||
*) fatal "Unrecognized option '${1}' passed to ${FUNCNAME[0]}. Exiting." ;;
|
||||
esac
|
||||
done
|
||||
shift $((OPTIND - 1))
|
||||
|
||||
local _startRegex="${1}"
|
||||
local _endRegex="${2}"
|
||||
local _input="${3}"
|
||||
local _output
|
||||
|
||||
if [[ ${_removeLines} == true ]]; then
|
||||
if [[ ${_greedy} == true ]]; then
|
||||
if [[ ${_caseInsensitive} == true ]]; then
|
||||
_output="$(sed -nE "/${_startRegex}/I,/${_endRegex}/Ip" "${_input}" | sed -n '2,$p' | sed '$d')"
|
||||
else
|
||||
_output="$(sed -nE "/${_startRegex}/,/${_endRegex}/p" "${_input}" | sed -n '2,$p' | sed '$d')"
|
||||
fi
|
||||
else
|
||||
if [[ ${_caseInsensitive} == true ]]; then
|
||||
_output="$(sed -nE "/${_startRegex}/I,/${_endRegex}/I{p;/${_endRegex}/Iq}" "${_input}" | sed -n '2,$p' | sed '$d')"
|
||||
else
|
||||
_output="$(sed -nE "/${_startRegex}/,/${_endRegex}/{p;/${_endRegex}/q}" "${_input}" | sed -n '2,$p' | sed '$d')"
|
||||
fi
|
||||
fi
|
||||
else
|
||||
if [[ ${_greedy} == true ]]; then
|
||||
if [[ ${_caseInsensitive} == true ]]; then
|
||||
_output="$(sed -nE "/${_startRegex}/I,/${_endRegex}/Ip" "${_input}")"
|
||||
else
|
||||
_output="$(sed -nE "/${_startRegex}/,/${_endRegex}/p" "${_input}")"
|
||||
fi
|
||||
else
|
||||
if [[ ${_caseInsensitive} == true ]]; then
|
||||
_output="$(sed -nE "/${_startRegex}/I,/${_endRegex}/I{p;/${_endRegex}/Iq}" "${_input}")"
|
||||
else
|
||||
_output="$(sed -nE "/${_startRegex}/,/${_endRegex}/{p;/${_endRegex}/q}" "${_input}")"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -n ${_output:-} ]]; then
|
||||
printf "%s\n" "${_output}"
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
)
|
||||
|
||||
_readFile_() {
|
||||
# DESC:
|
||||
# Prints each line of a file
|
||||
|
||||
@@ -243,7 +243,7 @@ _regexCapture_() {
|
||||
# 1 - Regex did not match
|
||||
# stdout: Prints string matching regex
|
||||
# USAGE:
|
||||
# HEXCODE=$(_regex_ "background-color: #FFFFFF;" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$')
|
||||
# HEXCODE=$(_regexCapture_ "background-color: #FFFFFF;" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$')
|
||||
# $ printf "%s\n" "${HEXCODE}"
|
||||
# $ #FFFFFF
|
||||
# NOTE:
|
||||
@@ -290,9 +290,9 @@ _rtrim_() {
|
||||
sed "s%[${_char//%/\\%}]*$%%"
|
||||
}
|
||||
|
||||
_splitString_() {
|
||||
_splitString_() (
|
||||
# DESC:
|
||||
# Splat a string into an array based on a given delimiter
|
||||
# Split a string into an array based on a given delimiter
|
||||
# ARGS:
|
||||
# $1 (Required) - String to be split
|
||||
# $2 (Required) - Delimiter
|
||||
@@ -302,15 +302,17 @@ _splitString_() {
|
||||
# stdout: Values split by delimiter separated by newline
|
||||
# USAGE:
|
||||
# ARRAY=( $(_splitString_ "string1,string2,string3" ",") )
|
||||
# CREDIT:
|
||||
# https://github.com/labbots/bash-utility/blob/master/src/misc.sh
|
||||
|
||||
[[ $# -lt 2 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||
|
||||
declare -a _arr=()
|
||||
IFS=$'\n' read -d "" -ra _arr <<<"${1//$2/$'\n'}"
|
||||
local _input="${1}"
|
||||
local _delimeter="${2}"
|
||||
|
||||
IFS="${_delimeter}" read -r -a _arr <<<"${_input}"
|
||||
|
||||
printf '%s\n' "${_arr[@]}"
|
||||
}
|
||||
)
|
||||
|
||||
_stringContains_() {
|
||||
# DESC:
|
||||
|
||||
Reference in New Issue
Block a user