Retrieve printer detail - AWK

Hi All,

For some reason, I need to have the printer detail from the AIX. Following are the part of /etc/qconfig and /etc/hosts. Can any one of you please help me with an awk some other option to get the detail ? I have mentioned the required output format at the end.

/etc/qconfig

s63lb1:
   device = @s63lb1
   host = s63lb1
   rq = zebra
   s_statfilter = /usr/lib/lpd/aixshort
   l_statfilter = /usr/lib/lpd/aixlong
@s63lb1:
   header = never
   trailer = never
   access = both
   backend = /usr/lib/lpd/pio/etc/piorlfb -f !
s63lb2:
   device = @s63lb2
   host = s63lb2
   rq = zebra
   s_statfilter = /usr/lib/lpd/aixshort
   l_statfilter = /usr/lib/lpd/aixlong
@s63lb2:
   header = never
   trailer = never
   access = both
   backend = /usr/lib/lpd/pio/etc/piorlfb -f !
s10comp2:
   device = @s10comp2
   host = s10comp2
   rq = HPLaserj
   s_statfilter = /usr/lib/lpd/aixshort
   l_statfilter = /usr/lib/lpd/aixlong
@s10comp2:
   header = never
   trailer = never
   access = both
   backend = /usr/lib/lpd/pio/etc/piorlfb -f !

/etc/hosts

192.168.100.122 s63lb1
192.168.100.123 s10comp2
192.168.100.124 roh_elc
192.168.100.125 s63lb2
192.168.100.126 roh_evl2
192.168.100.127 roh_lh750c
192.168.100.128 roh_star2
192.168.100.129 roh_arc2k2

Required out put format

Host_name   ip_address    RQ_Name
s63lb1      192.168.100.122   zebra
s63lb2     192.168.100.125 zebra 
s10comp2    192.168.100.123 HPLaserj 

Thank you in advance ...
Regards

Try:

awk 'NR==FNR{a[$2]=$1;next}/host/{h=$3}/rq/{r=$3}/^@/{print h,a[h],r}' /etc/hosts /etc/qconfig
1 Like

Hi sraj142,

One way:

$ cat script.awk
BEGIN {                                                                                                                                                                                                                                      
        ## Print header.                                                                                                                                                                                                                     
        printf "%-20s%-20s%-20s\n", "Host_name", "ip_address", "RQ_Name"                                                                                                                                                                     
}                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                             
## Save hosts with its IPs.                                                                                                                                                                                                                  
FNR == NR {                                                                                                                                                                                                                                  
        hostname[ $2 ] = $1                                                                                                                                                                                                                  
        next                                                                                                                                                                                                                                 
}                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                             
## Save host name when found a line with one field not beginning with '@'.                                                                                                                                                                   
FNR < NR && NF == 1 && substr( $1, 1, 1 ) != "@" {                                                                                                                                                                                           
        hname = substr( $1, 1, length( $1 ) - 1 )                                                                                                                                                                                            
        next;                                                                                                                                                                                                                                
}                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                             
## When found an entry beginning with 'rq', print all.                                                                                                                                                                                       
FNR < NR && $1 == "rq" {                                                                                                                                                                                                                     
        printf "%-20s%-20s%-20s\n", hname, hostname[ hname ], $3                                                                                                                                                                             
}                                                                                                                                                                                                                                            
$ awk -f script.awk /etc/hosts /etc/qconfig                                                                                                                                                                                                      
Host_name           ip_address          RQ_Name                                                                                                                                                                                              
s63lb1              192.168.100.122     zebra                                                                                                                                                                                                
s63lb2              192.168.100.125     zebra                                                                                                                                                                                                
s10comp2            192.168.100.123     HPLaserj
# awk 'BEGIN{printf "%-10s%20s%10s\n","Host_name","ip_address","RQ_Name"}{if(/^s/){s=$0;while(getline){if(/^@/)break;if(/rq/)s=s $0};
gsub(": *|rq =*","",s);host[x++]=s;next}}NR!=FNR{for(i=0;i<x;i++){split(host,b);if(b[1]~$2)printf "%-10s%20s%10s\n", $2,$1,b[2]}
}' /etc/qconfig /etc/hosts
Host_name           ip_address   RQ_Name
s63lb1         192.168.100.122     zebra
s10comp2       192.168.100.123  HPLaserj
s63lb2         192.168.100.125     zebra
1 Like