[SWLUG] Awk is driving me MAD!

Gareth James Powell gareth-j.powell at st.com
Wed Jun 11 08:08:25 UTC 2003


Hi all,

dick at acm.org wrote:
> 
> On Tue, 2003-06-10 at 17:34, Terry John wrote:
> > Try
> >
> > awk '{printf("%s",$2); printf(" IN A "); print $1}' /etc/hosts
> 
> or: awk '{printf("%s IN A %s\n", $2, $1);}' /etc/hosts
> 
> The original (awk '{print $2 " IN A " $1}' /etc/hosts) works for me,
> though :)
> 
> - Dick
> 

I get:

 IN A #
Internet IN A #
 IN A #
localhost IN A 127.0.0.1
 etc.

...as hosts on the works box I use has comment lines

#
# Internet host table
#
127.0.0.1       localhost
 etc.

 The line below takes care of comments[1] for me and reunites awk with
its old friend sed!

 cat /etc/hosts | sed "s/[#][a-zA-Z0-9_-+\\/t*,. ]*//g;" | \
 awk '{ if (length) { printf("%s",$2); printf(" IN A "); print $1}}' 

[1] works for comments that start on the first column, nadger the
regexp if YMHV[2] in some way.

[2] Your mileage has varied.

Regards,
Gareth.

-- 

"No matter how it looks at first, it's always a people problem." --
Gerald Weinberg, The Secrets of Consulting
 
 Gareth James Powell 
 Software Applications Engineer

 STB Division                    
|                                       
 STMicroelectronics (R&D) Limited |                    TINA: 065 2587
 1000 Aztec West                  |  direct dial: +44 (0)1454 462 587
 Almondsbury                      |  switchboard: +44 (0)1454 616 616
 BRISTOL                          |          Fax: +44 (0)1454 617 910
 BS32 4SQ                         | email:




More information about the Swlug mailing list