[CentOS] awk awk
Les Mikesell
lesmikesell at gmail.com
Fri Dec 7 05:45:48 UTC 2012
On Thu, Dec 6, 2012 at 3:48 PM, Craig White <craig.white at ttiltd.com> wrote:
>
> not that I was looking for someone to write it for me but that works only when the nginx.conf looks like
>
> server_name domain1.com domain2.com big.server.com;
>
> which I actually didn't need to use awk to parse as I already handled those instances just fine with grep/sed
>
> but I have some conf files which look like
>
> server_name {
> domain1.com
> domain2.com
> big.server.com
> }
> ;
>
> and that forced me into looking at alternative methods - hence awk
It's kind of hard to write a generic parser in regexps, but something
like this in perl should catch most of the likely layouts:
while (<>) {
chomp();
next if (m/^\s*#/); #comment
if ($enclosed) {
if (m/}/) {$enclosed = 0;} # end found
tr/}\;//d; #remove
push @servers, split(); #anything else on line
next;
} else {
next unless m/server_name/;
if (m/{/) { $enclosed = 1;}
if (m/}/) {$enclosed = 0;} #on same line?
s/server_name//;
tr/{}\;//d ;
push @servers,split();
}
}
foreach (sort(@servers)) { print "$_\n";}
--
Les Mikesell
lesmikesell at gmail.com
More information about the CentOS
mailing list