zsh-users
 help / color / mirror / code / Atom feed
* Help parsing a file from one regex to another
@ 2005-07-08  6:36 Travis Spencer
  2005-07-08  7:30 ` Doug Kearns
  0 siblings, 1 reply; 6+ messages in thread
From: Travis Spencer @ 2005-07-08  6:36 UTC (permalink / raw)
  To: zsh-users

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-07-09 17:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-08  6:36 Help parsing a file from one regex to another Travis Spencer
2005-07-08  7:30 ` Doug Kearns
2005-07-08 17:16   ` Bart Schaefer
2005-07-08 19:24     ` Travis Spencer
2005-07-09  6:41     ` Doug Kearns
2005-07-09 17:16       ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).