I have two snmpwalk outputs (non-sorted):
.248.194.136.249.4.240.0 = Counter32: 1
.248.194.136.249.4.240.1 = Counter32: 7
.248.194.136.249.8.112.0 = Counter32: 4
.248.194.136.249.8.112.1 = Counter32: 4
and
.248.194.136.249.4.240 = STRING: "building_1"
.248.194.136.249.8.112 = STRING: "building_2"
I'm trying to get an output similar to this:
building_1.0: 1
building_1.1: 7
building_2.0: 4
building_2.1: 4
I haven't been able to get this to work. Can this be achieved using grep within multiple awk statements? Is there a cleaner way to do it with awk?
EDIT: My attempts don't seem very relevant; since I'm uncertain if awk has the ability to do what I'm trying to do, I haven't been able to come up with a working attempt.
using awk
create a file named awkscript and add the following codes
FNR==NR{array[$0]=$0;next}
{gsub(/\"/,"")
for(i in array){
split(array[i],name)
match1=substr(name[1],0,22)
if(match1==$1){print $4 substr(name[1],23,2) ": " name[4]}
}
}
run script
awk -f path_to_awkscript path_to_fileA path_to_fileB
where fileA contains
.248.194.136.249.4.240.0 = Counter32: 1
.248.194.136.249.4.240.1 = Counter32: 7
.248.194.136.249.8.112.0 = Counter32: 4
.248.194.136.249.8.112.1 = Counter32: 4
and fileB contains
.248.194.136.249.4.240 = STRING: "building_1"
.248.194.136.249.8.112 = STRING: "building_2"
testing output
if fileA contains
.248.194.136.249.4.240.0 = Counter32: 1
.248.194.136.249.4.240.1 = Counter32: 7
.248.194.136.249.8.112.0 = Counter32: 4
.248.194.136.249.8.112.1 = Counter32: 4
.248.194.136.249.4.243.0 = Counter32: 1
.248.194.136.249.4.243.1 = Counter32: 10
.248.194.136.249.8.115.1 = Counter32: 13
and fileB contains
.248.194.136.249.4.240 = STRING: "building_1"
.248.194.136.249.8.112 = STRING: "building_2"
.248.194.136.249.8.115 = STRING: "building_10"
The output will be
building_1.1: 7
building_1.0: 1
building_2.1: 4
building_2.0: 4
building_10.1: 13
Note also
.248.194.136.249.4.243.0 = Counter32: 1
.248.194.136.249.4.243.1 = Counter32: 10
The above do not have a building address in fileB that corresponds to a line in fileA, therefore it is omitted
I'm not sure if this is the best way to do this, but this got me close enough to where I wanted to be:
awk -F"[.| ]" 'FNR==NR{f1[$2$3$4$5$6$7]=$0;next}$2$3$4$5$6$7 in f1{print f1[$2$3$4$5$6$7], $0}' file2 file1
Output:
.248.194.136.249.4.240 = STRING: "building_1" .248.194.136.249.4.240.0 = Counter32: 1
.248.194.136.249.4.240 = STRING: "building_1" .248.194.136.249.4.240.1 = Counter32: 7
.248.194.136.249.8.112 = STRING: "building_2" .248.194.136.249.8.112.0 = Counter32: 4
.248.194.136.249.8.112 = STRING: "building_2" .248.194.136.249.8.112.1 = Counter32: 4
Which can easily be manipulated into the output I was aiming for.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.