Re: awk awk

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



On Thu, Dec 6, 2012 at 3:48 PM, Craig White <craig.white@xxxxxxxxxx> 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@xxxxxxxxx
_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos


[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux