Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

awk insert coulmn upon match

SOLVED
Go to solution
DeafFrog
Valued Contributor

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
1 REPLY
Dennis Handly
Acclaimed Contributor
Solution

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