awk insert coulmn upon match (332 Views)
Reply
Valued Contributor
DeafFrog
Posts: 294
Registered: ‎02-25-2009
Message 1 of 2 (332 Views)
Accepted Solution

awk insert coulmn upon match

Hi Gurus ,

 

i have 2 csv as belwo :

 

apple:/home/techsup2/EVA#tail -5 LUN_VDISK_HOST_CSV.txt
 \Hosts\Linux\ciscomdr\1, \Virtual Disks\LINUX\tndlinux1\ACTIVE, \Hosts\Linux\ciscomdr
 \Hosts\Linux\ciscomrnd\1, \Virtual Disks\LINUX\tndlinux1\ACTIVE, \Hosts\Linux\ciscomrnd
 \Hosts\WINDOWS\IBNDB\1, \Virtual Disks\WINDOWS\BD_IBNDB\IBNDATA\ACTIVE, \Hosts\WINDOWS\IBNDB
 \Hosts\WINDOWS\IBNDB\2, \Virtual Disks\WINDOWS\BD_IBNDB\IBNINDEX\ACTIVE, \Hosts\WINDOWS\IBNDB
 \Hosts\HPUX\FINTEST\7, \Virtual Disks\HPUX\findev1--vgmodifytest\18-Oct-2011 11.03.47, \Hosts\HPUX\FINTEST
 
apple:/home/techsup2/EVA#tail -5 VDISK_WO_HOSTNAME_CSV.txt
 \Virtual Disks\LINUX\cistnd2-DR-u01\ACTIVE, 6001-4380-02a5-6541-0000-c000-0166-0000, 10
 \Virtual Disks\LINUX\cistnd2-Testlun5G\ACTIVE, 6001-4380-02a5-6541-0000-c000-00c1-0000, 5
 \Virtual Disks\LINUX\tndlinux1\ACTIVE, 6001-4380-02a5-64b7-0000-b000-006d-0000, 20
 \Virtual Disks\WINDOWS\BD_IBNDB\IBNDATA\ACTIVE, 6001-4380-02a5-64b7-0000-8000-003b-0000, 100
 \Virtual Disks\WINDOWS\BD_IBNDB\IBNINDEX\ACTIVE, 6001-4380-02a5-64b7-0000-8000-0040-0000, 60
apple:/home/techsup2/EVA#


for a match of  column 2 of LUN_VDISK_HOST_CSV.txt, ie vdisk name ,  with column 1 of VDISK_WO_HOSTNAME_CSV.txt ( which is not always the case)
i want to insert the coulm 2 and column 3 entry of VDISK_WO_HOSTNAME_CSV.txt into LUN_VDISK_HOST_CSV.txt.

 

Ideas , straight script all are wecome , thanks.

 

Rwgards,

FrogIsDeaf
Acclaimed Contributor
Dennis Handly
Posts: 25,071
Registered: ‎03-06-2006
Message 2 of 2 (313 Views)

Re: awk insert column upon match

awk -v file2=VDISK_WO_HOSTNAME_CSV.txt -F"," '
BEGIN {

   # save fields 2 & 3, with field 1 as key
   while (getline < file2 > 0) {
      save = $2 "," $3
      map[$1] = save
   }
   close file2
}
{

# if field2 in map, append value to end of line
insert = map[$2]
if (insert != "") {
   print $0 "," insert
}
}' LUN_VDISK_HOST_CSV.txt


Here is my output.  I'm appending fields 2&3 at the end of the first file's fields:

\Hosts\Linux\ciscomdr\1, \Virtual Disks\LINUX\tndlinux1\ACTIVE, \Hosts\Linux\ciscomdr, 6001-4380-02a5-64b7-0000-b000-006d-0000, 20
 \Hosts\Linux\ciscomrnd\1, \Virtual Disks\LINUX\tndlinux1\ACTIVE, \Hosts\Linux\ciscomrnd, 6001-4380-02a5-64b7-0000-b000-006d-0000, 20
 \Hosts\WINDOWS\IBNDB\1, \Virtual Disks\WINDOWS\BD_IBNDB\IBNDATA\ACTIVE, \Hosts\WINDOWS\IBNDB, 6001-4380-02a5-64b7-0000-8000-003b-0000, 100
 \Hosts\WINDOWS\IBNDB\2, \Virtual Disks\WINDOWS\BD_IBNDB\IBNINDEX\ACTIVE, \Hosts\WINDOWS\IBNDB, 6001-4380-02a5-64b7-0000-8000-0040-0000, 60

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.