[CentOS] awk awk

Craig White craig.white at ttiltd.com
Thu Dec 6 21:48:49 UTC 2012


On Dec 6, 2012, at 1:59 PM, m.roth at 5-cent.us wrote:

> Please stop top posting, Craig.
> 
> Craig White wrote:
>> 
>> On Dec 6, 2012, at 1:34 PM, m.roth at 5-cent.us wrote:
>> 
>>> You rang?
>>> 
>>> Craig White wrote:
>>>> a little out of my comfort zone and have practically gotten what I want
>>>> but awk seems determined to send a message via std error which is
>>>> problematic and annoying. Basically trying to get a list of virtual
>>>> host
>>>> names from nginx config files like this:
>>>> 
>>>> $ awk -F" " '/./ { if ( match ( "^server_name$", $2 ) ) print $1 }'
>>>> /opt/nginx/sites/*.conf \
>>>> | grep -v server_name | grep -v ';' | grep -v '}'
>>> 
>>> Why are you doing all that piping and grepping? And the -F" " confuses
>>> me...oh, I see. First, whitespace is the default field separator in awk.
>>> Then, are you asking if there's a line with a "." in it, or just any
>>> non-whitespace? If the latter... mmm, I see, you *really* don't
>>> understand
>>> awk.
>>> 
>>> awk -f '{if ( $1 ~ /server_name/ ) {\
>>>           server = $2;\
>>>           gsub(/;|}/,"",server);\
>>>           print server;
>>>        }
>>>       }'
>>> <snip>
>>>      mark
>>> 
>> Definitely have little to no understanding of awk but…
>> 
>> /./ suppresses empty lines (records in awk speak)
> 
> Oh. Never used it. Wrote a *lot* of *long* awk scripts over the years. But
> it doesn't matter - looking for $1 to be == server_name will only pick
> those lines.
>> 
>> the gsub looks interesting but your code just tosses syntax errors
> 
> I see I didn't out \ on the lines, which I wrote that way only to make it
> more readable.
>> 
>> and yes Les, the >2 /dev/null definitely redirected the awk squawk to
>> where it belonged
>> 
> Ok, I just d/l an nginx.conf file from <http://wiki.nginx.org/FullExample>
> and ran the following script on it:
> {
>   if ( $1 ~ /server_name$/ ) {
>      server = $2;
>      gsub(/;|}/,"",server);
>      print server;
>   }
> }
> 
> and my o/p was
> $ awk -f nginx.awk nginx.conf
> domain1.com
> domain2.com
> big.server.com
----
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

but your program gives me the following output…

$ awk -f nginx.awk /opt/nginx/sites/ids.conf 



$

Craig


More information about the CentOS mailing list