Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Help parsing a file from one regex to another



Hey,

I am trying to parse a range of data out of a file from one regular
expression up to another.  The input starts with `^@main::FLAGS' and
goes up to the next `)'  I am using awk right now, but it is *ugly*
and slow.  Does anyone have an suggestions on ways to speed it up and
beautify it?

TIA.

-- 

Regards,

Travis Spencer
Portland, OR USA

#!/bin/zsh

local flags tmp_file=${TEMP:-/tmp}/test$$

cat <<EOF > $tmp_file
# Where is the cache
$main::CACHEHOST        = "zok.cat.pdx.edu";
$main::CACHEPORT        = "5001";

# Name of flags
@main::FLAGS            = ( "ADMIN", "ARG", "CRACK", "DB", "DESKCATS",
                    "DROID", "ESHED", "HISS", "IMP", "KEYS",
                    "LABRES", "LINUX", "LOST", "LRP", "MAIL",
                    "MSDNAA", "NETWORK", "OIT", "PREFILTER",
                    "PRINTER", "PRINTING", "RESTORE",
                    "SECURITY", "SOFT", "SPAM", "TIER3",
                    "TUTOR", "UNIX", "WEB", "WINTEL");

# Name of priorities
@main::PRIORITIES       = ("GENERAL", "HOLD", "HOT", "STICKY", "WAIT");
EOF

function getflags {
    local config_file line_num

    config_file=${1-$tmp_file}
    line_num=$(grep -n main::FLAGS $config_file)
    flags=( $(
        awk '{
            if (NR >= line_num - 0) {
                if (/main::FLAGS/) {
                    for (i = 4; i <= NF; i++) {
                        print gensub(/"([^"]*)"(,|\);)/, "\\1", "s", $(0 + i));
                    }
                } else {
                    for (i = 1; i <= NF; i++) {
                         print gensub(/"([^"]*)"(,|\);)/, "\\1", "s", $(0 + i));
                    }
                }

                if (/);[[:blank:]]*$/) {
                    exit
                }
            }
        }' line_num=$line_num $config_file
    ) )
}

getflags
printf '>>%s<<\n' "${flags[@]}"
/bin/rm $tmp_file



Messages sorted by: Reverse Date, Date, Thread, Author