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!

Looking for guidance (correlation) 1

Status
Not open for further replies.

Kipnep70

Technical User
Nov 18, 2003
81
US
Here what I'm trying to do:

I have one file that has the following:

Code:
F001AE50C1      ADIC1_5_3_1_51  51      server1
F001AE5133      ADIC1_5_3_1_52  52      server1
F001AE5139      ADIC1_5_4_2_53  53      server1
F001AE513F      ADIC1_5_4_2_54  54      server1

I have another file that looks like this:

Code:
Serial Number: F001AE50D3
Device Name  : /dev/rmt/c16t0d4BESTnb
-
Serial Number: F001AE50CD
Device Name  : /dev/rmt/c16t0d3BESTnb
-
Serial Number: F001AE50C7
Device Name  : /dev/rmt/c16t0d2BESTnb
-
Serial Number: F001AE50C1
Device Name  : /dev/rmt/c16t0d1BESTnb


What I would like to do is compare the two files; if I have any serial numbers that match then I'm trying to plugin the information in a command syntax.

For example, serial F001AE50C1 matches -- I would then like to assign some variables:

Code:
serial=F001AE50C1
drv_name=ADIC1_5_3_1_51
drv_num=51
device_name=/dev/rmt/c16t0d1BESTnb
server=server1


Once I get the variables set then then I can plug them into a command line to update a program's configuration. (netbackup)

Can anybody offer some guidance on which tool would best be suited to do this? (awk, ksh, perl?) Any additional help would also be appreciated.

-Unix Beginner
 
Hi

Code:
awk -F" *: *" 'NR==FNR{if($1=="Serial Number")s=$2;if($1=="Device Name")d[s]=$2;next}FNR==1{FS=" ";$0=$0}$1 in d{print"serial="$1;print"drv_name="$2;print"drv_num="$3;print"device_name="d[$1];print"server="$4}' /input/file2 /input/file1
Note the order of input files. ( I numbered them as you posted the samples. )

Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

Feherke.
 
Thanks feherke,

One more thing ... Can you add a number on the end of the variable name to build an array?

Example:

serial[0]=RB0519AMC05153
drv_name[0]=SDLT600_7
drv_num[0]=7
device_name[0]=/dev/rmt/c52t3d0BESTnb
server[0]=uhilhpk7
serial[1]=RB0551AMC05389
drv_name[1]=SDLT600_8
drv_num[1]=8
device_name[1]=/dev/rmt/c49t3d0BESTnb
server[1]=uhilhpk7

I know you probably just have to stick a counter in the awk command but I'm not familiar enough with your syntax to know where to place it.

Thanks you again for your help on this. You don't know how much time this is going to save me.
 
This is my pathetic attempt ...

# for x in $(cat /tmp/drv_var); do
> n=0
> sed "s/=/${n}=/"
> ((n=n+1))
> done


Not working ...
 
Hi

As far as I understand, you want that numbering only in the output. So you only have to include it in the [tt]print[/tt]. ( But I changed the 5 [tt]print[/tt]s to 1 [tt]printf[/tt]. It looks cleaner this way. )
Code:
awk -F" *: *" 'NR==FNR{if($1=="Serial Number")s=$2;if($1=="Device Name")d[s]=$2;next}FNR==1{FS=" ";$0=$0}$1 in d{printf"serial[%d]=%s\ndrv_name[%d]=%s\ndrv_num[%d]=%s\ndevice_name[%d]=%s\nserver[%d]=%s\n",n,$1,n,$2,n,$3,n,d[$1],n,$4;n++}' /input/file2 /input/file1


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top