Страница 1 из 1
регулярные выражения в шелл
Добавлено: 2009-01-05 19:08:33
Alex Keda
сабж.
надо разобрать файл содержащий секции типа:
Код: Выделить всё
Account "h1526" {
Owner root
Domain xxx.ru
Ip 78.x.x.122
Preset ID50
SSL off
PHP fcgi
CGI on
SSI off
DiskLimit 0
UserLimit 0
CPULimit 10
MemLimit 16
ProcLimit 3
FtpLimit 1
MailLimit 1
DomainLimit 1
WebDomainLimit 1
MailDomainLimit 1
DatabaseLimit 1
DatabaseUserLimit 1
BandwidthLimit 10000
BandwidthUsage 0
}
В идеале, конечно в переменные, практически - хотя бы подробить на отдельные файлы по имени юзера (в примере - h1526) - дальше мона грепом выдернуть нужную строку...
пока додумался тока до
а вот дальше - идеи пропали... сделал всё в одну строку - а как побить на куски - даж не знаю =((
Re: регулярные выражения в шелл
Добавлено: 2009-01-05 19:33:19
hizel
да воспользуйся чем нибудь более высокоуровневым, например php

Re: регулярные выражения в шелл
Добавлено: 2009-01-05 19:43:36
zg
Код: Выделить всё
zg# cat 1.awk
#!/usr/bin/awk -f
{
if ( $1== "Account" && $3== "{" )
{
section = $2
gsub(/"/, "", $2);
toFile = $2;
print "Output " section " section to file " toFile;
}
if ( toFile ) print $0 > toFile;
if ( $1 == "}" ) toFile = null;
}
zg#
Код: Выделить всё
zg# ./1.awk 1.txt
Output "h1526" section to file h1526
Output "h1523" section to file h1523
zg#
Код: Выделить всё
zg# cat h1523
Account h1523 {
Owner root
Domain xxx.ru
Ip 78.x.x.122
Preset ID50
SSL off
PHP fcgi
CGI on
SSI off
DiskLimit 0
UserLimit 0
CPULimit 10
MemLimit 16
ProcLimit 3
FtpLimit 1
MailLimit 1
DomainLimit 1
WebDomainLimit 1
MailDomainLimit 1
DatabaseLimit 1
DatabaseUserLimit 1
BandwidthLimit 10000
BandwidthUsage 0
}
zg#
Re: регулярные выражения в шелл
Добавлено: 2009-01-05 20:37:51
Alex Keda
бага какая-то
Код: Выделить всё
srv# ./test.awk /usr/local/ispmgr/etc/ispmgr.conf ~
Output "host-foodru" section to file host-foodru
Output "h1003" section to file h1003
Output "h1004" section to file h1004
Output "h1009" section to file h1009
Output "h1018" section to file h1018
Output "h1028" section to file h1028
Output "dspam" section to file dspam
Output "h1081" section to file h1081
Output "h1082" section to file h1082
Output "h1092" section to file h1092
Output "h1103" section to file h1103
Output "h1117" section to file h1117
Output "h1126" section to file h1126
Output "h1129" section to file h1129
Output "h1130" section to file h1130
Output "h1135" section to file h1135
Output "h1114" section to file h1114
Output "h1138" section to file h1138
/usr/bin/awk: h1138 makes too many open files
input record number 705, file /usr/local/ispmgr/etc/ispmgr.conf
source line number 10
Код: Выделить всё
srv# ll | grep h1 ~
-rw-r--r-- 1 root wheel - 336B 5 янв 20:32 h1003
-rw-r--r-- 1 root wheel - 345B 5 янв 20:32 h1004
-rw-r--r-- 1 root wheel - 340B 5 янв 20:32 h1009
-rw-r--r-- 1 root wheel - 426B 5 янв 20:32 h1018
-rw-r--r-- 1 root wheel - 335B 5 янв 20:32 h1028
-rw-r--r-- 1 root wheel - 339B 5 янв 20:32 h1081
-rw-r--r-- 1 root wheel - 423B 5 янв 20:32 h1082
-rw-r--r-- 1 root wheel - 342B 5 янв 20:32 h1092
-rw-r--r-- 1 root wheel - 335B 5 янв 20:32 h1103
-rw-r--r-- 1 root wheel - 340B 5 янв 20:32 h1114
-rw-r--r-- 1 root wheel - 336B 5 янв 20:32 h1117
-rw-r--r-- 1 root wheel - 337B 5 янв 20:32 h1126
-rw-r--r-- 1 root wheel - 340B 5 янв 20:32 h1129
-rw-r--r-- 1 root wheel - 342B 5 янв 20:32 h1130
-rw-r--r-- 1 root wheel - 336B 5 янв 20:32 h1135
srv#
Re: регулярные выражения в шелл
Добавлено: 2009-01-05 20:52:08
zg
Код: Выделить всё
#!/usr/bin/awk -f
{
if ( $1== "Account" && $3== "{" )
{
if ( toFile ) close(toFile);
toFile = $2;
gsub(/"/, "", toFile);
print "Output " $2 " section to file " toFile;
}
if ( toFile ) print $0 > toFile;
if ( $1 == "}" )
{
if ( toFile ) close(toFile);
toFile = 0;
}
}
?
Re: регулярные выражения в шелл
Добавлено: 2009-01-05 20:54:14
Alex Keda
it's work!
thanks!
Re: регулярные выражения в шелл
Добавлено: 2009-01-05 21:28:21
Гость
lissyara писал(а):в переменные
ась?
Код: Выделить всё
#! /bin/sh
parse() {
while read par val; do
case $val in
*{*)
val=${val%\"*}
val=${val#\"}
root=$par
tree=${root}_$val
# cons=${root}_$tree
roots="$roots $root"
eval ${root}_trees=\"\$${root}_trees $tree\"
while read par val; do
case $par in
*}*) break
esac
branch=$par
leaf=$val
eval ${tree}_branches=\"\$${tree}_branches $branch\"
eval ${tree}_$branch=$leaf
done
esac
done
}
for arg; {
parse < $arg
}
echo roots=$roots
# show trees on root `Account'
echo Account_trees=$Account_trees
# show branches on tree `Account_h1526'
echo Account_h1526_branches=$Account_h1526_branches
# show leaf value on branch `Account_h1526_CGI'
echo Account_h1526_CGI=$Account_h1526_CGI
# test
echo Account_h1526_Owner=$Account_h1526_Owner
echo Account_dummy_Owner=$Account_dummy_Owner
Код: Выделить всё
(~). foo.sh data
roots= Account Account
Account_trees= Account_h1526 Account_dummy
Account_h1526_branches= Owner Domain Ip Preset SSL PHP CGI SSI DiskLimit UserLimit CPULimit MemLimit ProcLimit FtpLimit MailLimit DomainLimit WebDomainLimit MailDomainLimit DatabaseLimit DatabaseUserLimit BandwidthLimit BandwidthUsage
Account_h1526_CGI=on
Account_h1526_Owner=root
Account_dummy_Owner=luser
Re: регулярные выражения в шелл
Добавлено: 2009-01-05 21:30:47
Гость
Гость писал(а):roots= Account Account
упс, забыл засунуть алгоритм от дубликатов. Но я думаю сам разберешься как его сделать.
ps, awk в топку ;)
Re: регулярные выражения в шелл
Добавлено: 2009-01-05 21:33:20
zg
Гость писал(а):ps, awk в топку

ну да, на еволах лучше -))))