From d3093d4d72283019226ab0f8f76de520a0509e2a Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 14 Feb 2022 00:35:47 +0000 Subject: made parseconf insanely fast --- src/parseconf.sh | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/parseconf.sh b/src/parseconf.sh index aa5885d..f92bf0e 100755 --- a/src/parseconf.sh +++ b/src/parseconf.sh @@ -28,7 +28,7 @@ parse_line() { shift local value="$@" - [ "$key" = "include" ] && parse $value && return + [ "$key" = "include" ] && cat $value | parse && return [ "$key" = "]" ] && unset list[-1] && printf "\n" && return [ "$key" = "}" ] && unset level[-1] && return @@ -53,30 +53,13 @@ parse_line() { # parse () { local file="$1" + export level=() export list=() while IFS= read -r line; do # strip whitespace - line=$(sed "s/^#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" <<< "$line") - parse_line $line - done < "$file" -} - -# Filter the parsed file for specific keys -# -filter () { - local pattern= - - [ $# = 0 ] && - pattern=".*" || - pattern=$(sed "s/\*/[^:]*/g"<<< "$@") - - $print_keys && - pattern="s/^($pattern:.+)/\1/p" || - pattern="s/^$pattern:(.+)/\1/p" - - - parse $CONF_FILE | sed -rn $pattern + parse_line $line + done < "/dev/stdin" } # Use the env variable if exists @@ -98,7 +81,7 @@ while getopts ":f:c:v" opt; do print_keys=false ;; c) - count="${OPTARG}" + count="${OPTARG}q" ;; *) esac @@ -106,12 +89,12 @@ done shift $((OPTIND-1)) -if echo $@ | grep -q "."; then - [ -z ${count} ] && - filter "$@" || - filter "$@" | tail -n $count -else - parse $CONF_FILE -fi +[ $# = 0 ] && + pattern=".*" || + pattern=$(sed "s/\*/[^:]*/g" <<< "$@") +$print_keys && + pattern="s/^($pattern:.+)/\1/p;${count}" || + pattern="s/^$pattern:(.+)/\1/p;${count}" +sed "s/^#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" $CONF_FILE | parse $@ | sed -rn $pattern -- cgit v1.2.1