Depocu Depocu - 7 months ago 16
Bash Question

Bash compare previous variable

I am trying to parse csv file and rewrite with extra field, you can see csv file below

in the file you will see 192.168 ip address and 10.0 ip address.
192.168 is end point address and 10.0 is voip addresses.

If Switch ID,Switch Port ID,Description are equal it means 10.0 ip is voip phone for 192.168 ip address

for example;

192.168.205.76,189,FC3F.DB02.ED78,basement-k001a-asw1,GigabitEthernet3/1,basement-access,K022E-NB1-C1
10.0.40.46,1640,F025.7279.6DAA,basement-k001a-asw1,GigabitEthernet3/1,basement-access,K022E-NB1-C1


they are on both basement-k001a-asw1 and GigabitEthernet3/4,K013-EB3-C1.
So 10.0.40.29 is the voip phone for 192.168.189.26 endpoint.

What i am trying to do if ip start with 10.0 and its switchid,portid and description match previous one then i would like to write voip ip end of line of 192.168 line.

I can use global variable and use previous value, current value and change them as "for loop" continues
Example:

#!/bin/bash
previuos_ip=
current_ip=
previous_location=
current_location=

for systems in $(cat list.csv)

do
previous_ip=$current_ip
current_ip=$(echo "$systems" | cut -d, -f1)
previous_location=$current_location
current_location=$(echo "$systems" | cut -d, -f4,5,7)

printf "$previous_ip,$current_ip,$previous_location,$current_location\n"
done

previous_ip=$current_ip
current_ip=
previous_location=$current_location
current_location=


I hope i explained well. Any help appreciated
Thanks

Here is sample csv I have.

192.168.205.76,189,FC3F.DB02.ED78,basement-k001a-asw1,GigabitEthernet3/1,basement-access,K022E-NB1-C1
10.0.40.46,1640,F025.7279.6DAA,basement-k001a-asw1,GigabitEthernet3/1,basement-access,K022E-NB1-C1
10.68.194.185,1189,9C93.4E2D.EE1A,basement-k001a-asw1,GigabitEthernet3/3,basement-access,K022D-NB2-C2
192.168.189.26,189,9C8E.99DD.A49F,basement-k001a-asw1,GigabitEthernet3/4,basement-access,K013-EB3-C1
10.0.40.29,1640,1CDE.A783.EA7B,basement-k001a-asw1,GigabitEthernet3/4,basement-access,K013-EB3-C1
192.168.189.230,189,EC9A.7435.2177,basement-k001a-asw1,GigabitEthernet3/6,basement-access,K024-SB1-C1
192.168.189.34,189,70F3.95C1.11F8,basement-k001a-asw1,GigabitEthernet3/8,basement-access,K020-CF7-C1
10.0.40.45,1640,0008.2FB7.6F84,basement-k001a-asw1,GigabitEthernet3/11,basement-access,K002A-NB1-C1
192.168.189.22,189,8851.FB82.5DE3,basement-k001a-asw1,GigabitEthernet3/12,basement-access,K022D-NB1-C2
10.0.40.28,1640,3CCE.73AC.ED44,basement-k001a-asw1,GigabitEthernet3/12,basement-access,K022D-NB1-C2
192.168.189.225,189,9C93.4E4D.1DDA,basement-k001a-asw1,GigabitEthernet3/13,basement-access,K022D-NB2-C1
10.68.189.182,1189,001C.9B09.0504,basement-k001a-asw1,GigabitEthernet3/15,basement-access,K006-NW1-C1
10.0.40.42,1640,1CDE.A783.B19B,basement-k001a-asw1,GigabitEthernet3/16,basement-access,K005-NB1-C1
10.68.189.181,1189,9C93.4E16.D940,basement-k001a-asw1,GigabitEthernet3/17,basement-access,K004-WB1-C2
192.168.189.233,1189,9C93.4E67.2017,basement-k001a-asw1,GigabitEthernet3/27,basement-access,K013-SB1-C1
10.68.189.52,1189,0040.580D.157E,basement-k001a-asw1,GigabitEthernet3/28,basement-access,K009HALL-EW5-C1(KRONOS)
192.168.189.31,189,984B.E17D.5BE1,basement-k001a-asw1,GigabitEthernet3/34,basement-access,K013-WB1-C1
192.168.189.222,189,68B5.9941.32CE,basement-k001a-asw1,GigabitEthernet3/35,basement-access,K004-NB3-C1
10.0.40.56,1640,0CD9.9691.B9C3,basement-k001a-asw1,GigabitEthernet3/36,basement-access,K024HALL-WW1-C1
192.168.189.223,189,3CD9.2B0F.E714,basement-k001a-asw1,GigabitEthernet3/39,basement-access,K006-EB1-C2
10.0.40.44,1640,1CDE.A782.1A7E,basement-k001a-asw1,GigabitEthernet3/41,basement-access,K011-NB1-C2
192.168.189.224,189,1458.D039.9735,basement-k001a-asw1,GigabitEthernet3/42,basement-access,K013-WB2-C2
192.168.189.23,189,D4C9.EFD8.1490,basement-k001a-asw1,GigabitEthernet3/43,basement-access,K013-WB2-C1
10.0.40.30,1640,1CDE.A783.A7CD,basement-k001a-asw1,GigabitEthernet3/43,basement-access,K013-WB2-C1
192.168.189.25,189,8851.FB81.72E4,basement-k001a-asw1,GigabitEthernet3/44,basement-access,K002A-WB1-C2
192.168.189.29,189,D4C9.EFD3.E39B,basement-k001a-asw1,GigabitEthernet3/45,basement-access,K002A-WB1-C1
10.0.40.22,1640,3820.5618.1630,basement-k001a-asw1,GigabitEthernet3/45,basement-access,K002A-WB1-C1
10.0.40.39,1640,3820.5618.169B,basement-k001a-asw1,GigabitEthernet3/46,basement-access,K002A-SB1-C2
192.168.189.221,189,001A.4B1C.F810,basement-k001a-asw1,GigabitEthernet3/46,basement-access,K002A-SB1-C2
192.168.189.27,189,F4CE.4613.FF62,basement-k001a-asw1,GigabitEthernet3/47,basement-access,K002A-SB1-C1
10.0.40.25,1640,1CDE.A783.A92C,basement-k001a-asw1,GigabitEthernet3/47,basement-access,K002A-SB1-C1
172.16.45.183,45,0040.1135.7FC6,zph-04721-asw1,GigabitEthernet1/0/15,zph-access,04740-WB1-C1(SECURITY)
10.50.10.183,1045,0040.1935.7AC2,zph-04721-asw1,GigabitEthernet1/0/15,zph-access,04740-WB1-C1(SECURITY)
172.16.45.241,45,00C0.B792.8CD1,zph-04721-asw1,GigabitEthernet1/0/25,zph-access,04721-NETBOTZ
10.50.10.241,1045,1AD1.B792.8AD1,zph-04721-asw1,GigabitEthernet1/0/25,zph-access,04721-NETBO
192.168.189.2,189,00C0.B7B6.3A1A,basement-k001a-asw1,GigabitEthernet3/48,basement-access,Connectiontobasement-k001a-ups1


192.168.x.x and 172.16.x.x are endpoint

10.0.x.x and 10.50.x.x are phones.

Location match $4,$5,&7

expected result.

192.168.205.76,189,FC3F.DB02.ED78,basement-k001a-asw1,GigabitEthernet3/1,basement-access,K022E-NB1-C1,10.0.40.46
10.0.40.46,1640,F025.7279.6DAA,basement-k001a-asw1,GigabitEthernet3/1,basement-access,K022E-NB1-C1,N/A
10.68.194.185,1189,9C93.4E2D.EE1A,basement-k001a-asw1,GigabitEthernet3/3,basement-access,K022D-NB2-C2,N/A
192.168.189.26,189,9C8E.99DD.A49F,basement-k001a-asw1,GigabitEthernet3/4,basement-access,K013-EB3-C1,10.0.40.29
10.0.40.29,1640,1CDE.A783.EA7B,basement-k001a-asw1,GigabitEthernet3/4,basement-access,K013-EB3-C1,N/A
192.168.189.230,189,EC9A.7435.2177,basement-k001a-asw1,GigabitEthernet3/6,basement-access,K024-SB1-C1,N/A
192.168.189.34,189,70F3.95C1.11F8,basement-k001a-asw1,GigabitEthernet3/8,basement-access,K020-CF7-C1,N/A
10.0.40.45,1640,0008.2FB7.6F84,basement-k001a-asw1,GigabitEthernet3/11,basement-access,K002A-NB1-C1,N/A
192.168.189.22,189,8851.FB82.5DE3,basement-k001a-asw1,GigabitEthernet3/12,basement-access,K022D-NB1-C2,10.0.40.28
10.0.40.28,1640,3CCE.73AC.ED44,basement-k001a-asw1,GigabitEthernet3/12,basement-access,K022D-NB1-C2,N/A
192.168.189.225,189,9C93.4E4D.1DDA,basement-k001a-asw1,GigabitEthernet3/13,basement-access,K022D-NB2-C1,N/A
10.68.189.182,1189,001C.9B09.0504,basement-k001a-asw1,GigabitEthernet3/15,basement-access,K006-NW1-C1,N/A
10.0.40.42,1640,1CDE.A783.B19B,basement-k001a-asw1,GigabitEthernet3/16,basement-access,K005-NB1-C1,N/A
10.68.189.181,1189,9C93.4E16.D940,basement-k001a-asw1,GigabitEthernet3/17,basement-access,K004-WB1-C2,N/A
192.168.189.233,1189,9C93.4E67.2017,basement-k001a-asw1,GigabitEthernet3/27,basement-access,K013-SB1-C1,N/A
10.68.189.52,1189,0040.580D.157E,basement-k001a-asw1,GigabitEthernet3/28,basement-access,K009HALL-EW5-C1(KRONOS),N/A
192.168.189.31,189,984B.E17D.5BE1,basement-k001a-asw1,GigabitEthernet3/34,basement-access,K013-WB1-C1,N/A
192.168.189.222,189,68B5.9941.32CE,basement-k001a-asw1,GigabitEthernet3/35,basement-access,K004-NB3-C1,N/A
10.0.40.56,1640,0CD9.9691.B9C3,basement-k001a-asw1,GigabitEthernet3/36,basement-access,K024HALL-WW1-C1,N/A
192.168.189.223,189,3CD9.2B0F.E714,basement-k001a-asw1,GigabitEthernet3/39,basement-access,K006-EB1-C2,N/A
10.0.40.44,1640,1CDE.A782.1A7E,basement-k001a-asw1,GigabitEthernet3/41,basement-access,K011-NB1-C2,N/A
192.168.189.224,189,1458.D039.9735,basement-k001a-asw1,GigabitEthernet3/42,basement-access,K013-WB2-C2,N/A
192.168.189.23,189,D4C9.EFD8.1490,basement-k001a-asw1,GigabitEthernet3/43,basement-access,K013-WB2-C1,10.0.40.30
10.0.40.30,1640,1CDE.A783.A7CD,basement-k001a-asw1,GigabitEthernet3/43,basement-access,K013-WB2-C1,N/A
192.168.189.25,189,8851.FB81.72E4,basement-k001a-asw1,GigabitEthernet3/44,basement-access,K002A-WB1-C2,N/A
192.168.189.29,189,D4C9.EFD3.E39B,basement-k001a-asw1,GigabitEthernet3/45,basement-access,K002A-WB1-C1,10.0.40.22
10.0.40.22,1640,3820.5618.1630,basement-k001a-asw1,GigabitEthernet3/45,basement-access,K002A-WB1-C1,N/A
10.0.40.39,1640,3820.5618.169B,basement-k001a-asw1,GigabitEthernet3/46,basement-access,K002A-SB1-C2,N/A
192.168.189.221,189,001A.4B1C.F810,basement-k001a-asw1,GigabitEthernet3/46,basement-access,K002A-SB1-C2,10.0.40.39
192.168.189.27,189,F4CE.4613.FF62,basement-k001a-asw1,GigabitEthernet3/47,basement-access,K002A-SB1-C1,10.0.40.25
10.0.40.25,1640,1CDE.A783.A92C,basement-k001a-asw1,GigabitEthernet3/47,basement-access,K002A-SB1-C1,N/A
172.16.45.183,45,0040.1135.7FC6,zph-04721-asw1,GigabitEthernet1/0/15,zph-access,04740-WB1-C1(SECURITY),10.50.10.183
10.50.10.183,1045,0040.1935.7AC2,zph-04721-asw1,GigabitEthernet1/0/15,zph-access,04740-WB1-C1(SECURITY),N/A
172.16.45.241,45,00C0.B792.8CD1,zph-04721-asw1,GigabitEthernet1/0/25,zph-access,04721-NETBOTZ,10.50.10.241
10.50.10.241,1045,1AD1.B792.8AD1,zph-04721-asw1,GigabitEthernet1/0/25,zph-access,04721-NETBO,N/A
192.168.189.2,189,00C0.B7B6.3A1A,basement-k001a-asw1,GigabitEthernet3/48,basement-access,Connectiontobasement-k001a-ups1,N/A

Answer

You can use awk to simplify this:

awk 'BEGIN{FS=OFS=","} {k=$4 FS $5 FS $7}
 $1~/^1[79]2\./{if (pr) print pr, "N/A"; pr=$0; pk=k}
 $1~/^10\./{if (k == pk) { print pr, $1; pr=""} print $0, "N/A"}
 END{if (pr) print pr, "N/A"}' file

Output:

192.168.205.76,189,FC3F.DB02.ED78,basement-k001a-asw1,GigabitEthernet3/1,basement-access,K022E-NB1-C1,10.0.40.46
10.0.40.46,1640,F025.7279.6DAA,basement-k001a-asw1,GigabitEthernet3/1,basement-access,K022E-NB1-C1,N/A
10.68.194.185,1189,9C93.4E2D.EE1A,basement-k001a-asw1,GigabitEthernet3/3,basement-access,K022D-NB2-C2,N/A
192.168.189.26,189,9C8E.99DD.A49F,basement-k001a-asw1,GigabitEthernet3/4,basement-access,K013-EB3-C1,10.0.40.29
10.0.40.29,1640,1CDE.A783.EA7B,basement-k001a-asw1,GigabitEthernet3/4,basement-access,K013-EB3-C1,N/A
192.168.189.230,189,EC9A.7435.2177,basement-k001a-asw1,GigabitEthernet3/6,basement-access,K024-SB1-C1,N/A
10.0.40.45,1640,0008.2FB7.6F84,basement-k001a-asw1,GigabitEthernet3/11,basement-access,K002A-NB1-C1,N/A
192.168.189.34,189,70F3.95C1.11F8,basement-k001a-asw1,GigabitEthernet3/8,basement-access,K020-CF7-C1,N/A
192.168.189.22,189,8851.FB82.5DE3,basement-k001a-asw1,GigabitEthernet3/12,basement-access,K022D-NB1-C2,10.0.40.28
10.0.40.28,1640,3CCE.73AC.ED44,basement-k001a-asw1,GigabitEthernet3/12,basement-access,K022D-NB1-C2,N/A
10.68.189.182,1189,001C.9B09.0504,basement-k001a-asw1,GigabitEthernet3/15,basement-access,K006-NW1-C1,N/A
10.0.40.42,1640,1CDE.A783.B19B,basement-k001a-asw1,GigabitEthernet3/16,basement-access,K005-NB1-C1,N/A
10.68.189.181,1189,9C93.4E16.D940,basement-k001a-asw1,GigabitEthernet3/17,basement-access,K004-WB1-C2,N/A
192.168.189.225,189,9C93.4E4D.1DDA,basement-k001a-asw1,GigabitEthernet3/13,basement-access,K022D-NB2-C1,N/A
10.68.189.52,1189,0040.580D.157E,basement-k001a-asw1,GigabitEthernet3/28,basement-access,K009HALL-EW5-C1(KRONOS),N/A
192.168.189.233,1189,9C93.4E67.2017,basement-k001a-asw1,GigabitEthernet3/27,basement-access,K013-SB1-C1,N/A
192.168.189.31,189,984B.E17D.5BE1,basement-k001a-asw1,GigabitEthernet3/34,basement-access,K013-WB1-C1,N/A
10.0.40.56,1640,0CD9.9691.B9C3,basement-k001a-asw1,GigabitEthernet3/36,basement-access,K024HALL-WW1-C1,N/A
192.168.189.222,189,68B5.9941.32CE,basement-k001a-asw1,GigabitEthernet3/35,basement-access,K004-NB3-C1,N/A
10.0.40.44,1640,1CDE.A782.1A7E,basement-k001a-asw1,GigabitEthernet3/41,basement-access,K011-NB1-C2,N/A
192.168.189.223,189,3CD9.2B0F.E714,basement-k001a-asw1,GigabitEthernet3/39,basement-access,K006-EB1-C2,N/A
192.168.189.224,189,1458.D039.9735,basement-k001a-asw1,GigabitEthernet3/42,basement-access,K013-WB2-C2,N/A
192.168.189.23,189,D4C9.EFD8.1490,basement-k001a-asw1,GigabitEthernet3/43,basement-access,K013-WB2-C1,10.0.40.30
10.0.40.30,1640,1CDE.A783.A7CD,basement-k001a-asw1,GigabitEthernet3/43,basement-access,K013-WB2-C1,N/A
192.168.189.25,189,8851.FB81.72E4,basement-k001a-asw1,GigabitEthernet3/44,basement-access,K002A-WB1-C2,N/A
192.168.189.29,189,D4C9.EFD3.E39B,basement-k001a-asw1,GigabitEthernet3/45,basement-access,K002A-WB1-C1,10.0.40.22
10.0.40.22,1640,3820.5618.1630,basement-k001a-asw1,GigabitEthernet3/45,basement-access,K002A-WB1-C1,N/A
10.0.40.39,1640,3820.5618.169B,basement-k001a-asw1,GigabitEthernet3/46,basement-access,K002A-SB1-C2,N/A
192.168.189.221,189,001A.4B1C.F810,basement-k001a-asw1,GigabitEthernet3/46,basement-access,K002A-SB1-C2,N/A
192.168.189.27,189,F4CE.4613.FF62,basement-k001a-asw1,GigabitEthernet3/47,basement-access,K002A-SB1-C1,10.0.40.25
10.0.40.25,1640,1CDE.A783.A92C,basement-k001a-asw1,GigabitEthernet3/47,basement-access,K002A-SB1-C1,N/A
172.16.45.183,45,0040.1135.7FC6,zph-04721-asw1,GigabitEthernet1/0/15,zph-access,04740-WB1-C1(SECURITY),10.50.10.183
10.50.10.183,1045,0040.1935.7AC2,zph-04721-asw1,GigabitEthernet1/0/15,zph-access,04740-WB1-C1(SECURITY),N/A
172.16.45.241,45,00C0.B792.8CD1,zph-04721-asw1,GigabitEthernet1/0/25,zph-access,04721-NETBOTZ,10.50.10.241
10.50.10.241,1045,1AD1.B792.8AD1,zph-04721-asw1,GigabitEthernet1/0/25,zph-access,04721-NETBOTZ,N/A
192.168.189.2,189,00C0.B7B6.3A1A,basement-k001a-asw1,GigabitEthernet3/48,basement-access,Connectiontobasement-k001a-ups1,N/A
Comments