Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data differences 1

Status
Not open for further replies.

grapes12

Technical User
Mar 2, 2010
124
ZA
I have the following awk command, reading data from my local server(sun5) and remote server(sun8).Each doing the same command and displaying the differences into a diffs.txt file.
Code:
awk 'BEGIN {while ( getline < "sun5-printers.txt") {arr[$0]++ } } { if (!($0 in a
rr ) ) { print } }' sun8-printers.txt > diffs.txt

problem is that its not picking up all differences and displaying them.
here is an example:
sun5-printers.txt file
Code:
device for 5001a10: lpd://172.25.5.124:515
device for 5001a11: lpd://172.25.5.155:515
device for 5001a55: lpd://172.25.6.31:515
device for 5001a77: lpd://172.25.12.34:515
device for 5001a88: lpd://172.25.6.86:515
device for 5001a99: lpd://172.25.6.31:515
sun8-printers.txt file
Code:
device for 5001a11: lpd://172.25.5.155:515
device for 5001a44: lpd://172.25.6.31:515
device for 5001a77: lpd://172.25.12.34:515
device for 5001a88: lpd://172.25.6.86:515
device for 5001a99: lpd://172.25.6.31:515
diffs.txt file
Code:
more diffs.txt
device for 0601dn1: lpd://172.25.53.32:515
device for 0601jc1: lpd://172.25.53.33:515
device for 0601la1: lpd://172.25.53.34:515
device for 0601ps1: lpd://172.25.53.36:515
device for 0601ps2: lpd://172.25.53.30:515
device for 0606ge1: lpd://172.28.9.29:515
device for 5001a44: lpd://172.25.6.31:515

NOTICE that its not picking up the 5001a55. Any advice on how to fix the awk command?????


 
man diff
man comm

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you...
so there is no way too change my awk command instead...?????
 
I adjusted my AWk command to look like this, which works
Code:
awk 'NR==FNR{arr[$0]=$0; next} $0 in arr {delete arr[$0]; next} {print} EN
D{for ( i in arr ) {print arr[i]}}' sun5-printers.txt sun8-printers.txt > diffs.txt

BUT it picks up duplicates, which i know exists due to the IP addresses which differ on the servers
diffs.txt file after i run the awk
Code:
more diffs.txt
device for 0601dn1: lpd://172.25.53.32:515
device for 0601jc1: lpd://172.25.53.33:515
device for 0601la1: lpd://172.25.53.34:515
device for 0601ps1: lpd://172.25.53.36:515
device for 0601ps2: lpd://172.25.53.30:515
device for 0606ge1: lpd://172.28.9.29:515
device for 5001a44: lpd://172.25.6.31:515
device for 0601dn1: lpd://172.25.9.102:515
device for device: ///dev/null
device for 0601jc1: lpd://172.25.9.104:515
device for 0601ps1: lpd://172.25.9.105:515
device for 0601la1: lpd://172.25.9.109:515
device for 0601ps2: lpd://172.25.9.110:515
device for 5001a55: lpd://172.25.6.31:515

 
The scenario i find myself in at the moment is this:
I have a script, that delete the printers from the diffs.txt file created, and then re-create the printers.
Code:
#!/bin/ksh
BIN=/usr/lbin
LOCAL_FILE=`$BIN/lpstat -v > sun5-printers.txt`
REMOTE_FILE=`rsh sun8 /usr/lbin/lpstat -v > sun8-printers.txt`
#DIFF_FILE=/usr/local/bin/diffs.txt

#awk 'BEGIN {while ( getline < "sun5-printers.txt") {arr[$0]++ } } { if (!($0 in
arr ) ) { print } }' sun8-printers.txt > diffs.txt

awk 'NR==FNR{arr[$0]=$0; next} $0 in arr {delete arr[$0]; next} {print} END{for (
 i in arr ) {print arr[i]}}' sun5-printers.txt sun8-printers.txt > diffs.txt

LINE='device for 0101a01: lpd://172.25.41.111:515'
while read LINE
do
  prt=`echo $LINE | awk '{print $3 }'| cut -c 1-7`
    echo $prt
  drv=`echo $LINE | awk -F":" '{print $2}'`
    echo $drv
  IP=`echo $LINE | awk -F"/" '{print $3}' | cut -d":" -f1`
    echo $IP
  port=`echo $LINE | awk -F":" '{print $4}'`
    echo $port

#Delete or Create Printer

   [ -z "${prt}" ] && continue

   echo "\n\t Deleting printer ${prt} !!!"

   if !  $BIN/lpadmin -x ${prt} 2> /dev/null
   then
       echo " Failed to delete printer !!"
       continue
   fi
   echo "Printer ${prt} DELETED!"

   echo "Adding Printer $prt !!!!"

   if ! $BIN/lpadmin -p ${prt} -E -v ${drv}://${IP}:${port}
   then
       echo "Failed to add printers !!!"
       continue
   fi

   echo "\n\t Printer $prt:$drv:$IP:$port created"

done < diffs.txt

But with duplicates it recreates my printers with the wrong IP addresses which should be used from the remote server(sun8) and its using the (sun5) IP addresses.????
Help will be much appreciated!!!!!
 
I've been doing some research for the last three hours..keep on running into a brick wall.

Can someone please tell me how I can fix the awk command not to produce the duplicates, and only show the differences????
 
Do you write *any* code yourself???


I don't mean any offence, but perhaps you're in the wrong job?

If you only want to consider the printer name in your comparisons, use $3 (the third field) instead of $0 (the entire line) as the index to your arr array.

Annihilannic.
 
None taken, but awk and sed are my nemesis!
I have tried that, using the $3(the third field), but now its ignoring the printer names that actually need changing.

 
It seems to work for me:

Code:
$ cat compare
awk '
        [blue]NR[/blue]==[blue]FNR[/blue] { arr[[blue]$3[/blue]]=[blue]$0[/blue]; [b]next[/b] }
        [blue]$3[/blue] [olive]in[/olive] arr { [olive]delete[/olive] arr[[blue]$3[/blue]]; [b]next[/b] }
        { [b]print[/b] }
        [green]END[/green] { [olive]for[/olive] (i [olive]in[/olive] arr) [b]print[/b] arr[i] }
' sun5-printers.txt sun8-printers.txt
$ cat sun5-printers.txt
device for 5001a10: lpd://172.25.5.124:515
device for 5001a11: lpd://172.25.5.155:515
device for 5001a55: lpd://172.25.6.31:515
device for 5001a77: lpd://172.25.12.34:515
device for 500dupe: lpd://172.25.12.34:515
device for 5001a88: lpd://172.25.6.86:515
device for 5001a99: lpd://172.25.6.31:515
$ cat sun8-printers.txt
device for 5001a11: lpd://172.25.5.155:515
device for 500dupe: lpd://172.25.43.21:515
device for 5001a44: lpd://172.25.6.31:515
device for 5001a77: lpd://172.25.12.34:515
device for 5001a88: lpd://172.25.6.86:515
device for 5001a99: lpd://172.25.6.31:515
$ sh compare
device for 5001a44: lpd://172.25.6.31:515
device for 5001a55: lpd://172.25.6.31:515
device for 5001a10: lpd://172.25.5.124:515
$

Note that 500dupe is not reported, even though its device IP address is different in each input file.

Annihilannic.
 
Okay thanks Annihilannic, it displays the difference between the two files! Any reason why the 500dupe will not be displayed?????
 
Because that's what you asked for??? You didn't want it to display duplicates, even if the device IP addresses were different... correct?

Or are you asking why it isn't displayed? It's because we are only comparing the printer names in the input files, not the entire line which includes the IP address.

Annihilannic.
 
Thanks a million, its so much clearer now..
just one last thing.

I compared my awk to yours and noticed that i have extra Curly brackets at the {PRINT arr}}', does it make any difference or not???

Code:
awk 'NR==FNR{arr[$3]=$0; next} $3 in arr {delete arr[$3]; next} {print} END{for (
 i in arr ) {print arr[i]}}' sun5-printers.txt sun8-printers.txt > diffs.txt
 
If you only wish to execute one statement when an if statement is true, they are not required. But if you have multiple statements that you want to execute when the condition is true, you must enclose them in braces (squiggly/curly/flower brackets).

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top