Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Help parsing a file from one regex to another
- X-seq: zsh-users 9056
- From: Travis Spencer <travislspencer@xxxxxxxxx>
- To: zsh-users@xxxxxxxxxx
- Subject: Help parsing a file from one regex to another
- Date: Thu, 7 Jul 2005 23:36:12 -0700
- Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=qWlifQ/J9mW4r/Y6Gf7h8+N92Sa1MUiU9DZVvEyJydKoE2/mmoZ4I2JHklXafnwZ2jKxorJTCP4eCHlobo6JRlzbfnJX8qUoUDZJOmuRtgYVZOCuw6xHvIcOX3F0ZzHHdGfbyh9BPpzrocJsLGG3ENxcJYbI1tN/zyIbF5gf4FM=
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- Reply-to: Travis Spencer <travislspencer@xxxxxxxxx>
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