Solved

Regex question & awk

Posted on 2013-12-17
11
239 Views
Last Modified: 2013-12-17
How should i export bold parts from the given text below

IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.185.9.159.252 = STRING: 0:25:90:a8:9a:ef

and there is one additional thing if it has one char between two double dot  as this

IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.185.9.158.25 = STRING: 0:4:ac:e3:e8:49

it will export 00:04:ac:e3:e8:49 i mean it will add a 0 to infront of alone char between dots


IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.185.9.159.252 = STRING: 0:25:90:a8:9a:ef
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.185.9.159.253 = STRING: 0:25:90:a8:9a:ef
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.185.9.159.254 = STRING: 0:50:56:be:f1:5f
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.192.168.1.104 = STRING: 2:d0:68:12:4b:cc

Open in new window

0
Comment
Question by:3XLcom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 39724487
So from this text,
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.185.9.159.252 = STRING: 0:25:90:a8:9a:ef

You want to extract IP address and MAC, this is all what I see is bolded?


Something like?

echo 'IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.185.9.159.252 = STRING: 0:25:90:a8:9a:ef' | sed 's/.*ipv4\.\([0-9\.]*\).*=.*STRING: \(.*\)/\1 \2/'

Open in new window

0
 

Author Comment

by:3XLcom
ID: 39724522
i need to export this from a text file
0
 
LVL 31

Expert Comment

by:farzanj
ID: 39724539
Export??  Adding 0 in front?  Also for IP?  Export where?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:3XLcom
ID: 39724592
my server generates tooo long text files as this and i want to export a result as :


172.16.1.21  ->  0:18:6e:37:cf:28
172.16.1.22  ->  00:01:e8:d6:53:37

but one important point on the text 00:01:e8:d6:53:37 that seems as 0:1:e8:d6:53:37 if it has one decimal between two dots it will add one more zero to infront of it



IP-MIB::ipNetToPhysicalPhysAddress.1107755015.ipv4.172.16.1.21 = STRING: 0:18:6e:37:cf:28
IP-MIB::ipNetToPhysicalPhysAddress.1107755015.ipv4.172.16.1.22 = STRING: 0:1:e8:d6:53:37
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.10.1.1.1 = STRING: 0:1:e8:d6:53:37
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.3 = STRING: 0:50:56:be:70:d8
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.5 = STRING: 0:4:ac:e3:e8:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.6 = STRING: 0:4:ac:e3:e8:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.9 = STRING: 2:d0:68:12:4b:cc
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.18 = STRING: 90:2b:34:9d:53:cb
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.19 = STRING: 90:2b:34:9d:53:cb
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.20 = STRING: 90:2b:34:a0:42:f3
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.21 = STRING: 90:2b:34:a0:42:f3
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.34 = STRING: e0:69:95:2e:90:a4
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.35 = STRING: e0:69:95:2e:90:a4
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.36 = STRING: 90:2b:34:a0:42:f3
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.39 = STRING: e0:69:95:2e:90:a4
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.67 = STRING: b8:ac:6f:97:82:6f
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.116 = STRING: 0:4:ac:e3:e8:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.162 = STRING: 0:50:56:be:36:c1
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.178 = STRING: 0:50:56:96:50:fa
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.179 = STRING: 0:50:56:96:3:8b
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.180 = STRING: 0:50:56:96:3:c3
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.181 = STRING: 0:50:56:96:3:8b
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.182 = STRING: 0:50:56:96:3:8b
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.183 = STRING: 0:50:56:96:3:8b
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.184 = STRING: 0:50:56:96:3:c3
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.185 = STRING: 0:50:56:96:25:99
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.188 = STRING: 0:50:56:96:3:c3
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.189 = STRING: 0:50:56:96:3:8b
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.226 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.227 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.228 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.229 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.230 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.231 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.232 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.233 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.234 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.235 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.236 = STRING: 0:50:56:be:d0:49

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
ID: 39724864
Ok, export word is still confusing.

Try this command:

perl -ne 'if(($a,$b)=m{ipv4\.(\S+)\s=\sSTRING:\s(\S+)}) { $b =~ s/\b(\d)\b/0$1/g; print "$a --> $b\n";} logfilename'

Open in new window


change logfilename to the filename of your log file.

See what you get.  If you are happy with the output, you can redirect it to any file.
0
 

Author Comment

by:3XLcom
ID: 39724879
This is the result :

[root@sflow islemler]# perl -ne 'if(($a,$b)=m{ipv4\.(\S+)\s=\sSTRING:\s(\S+)}) { $b =~ s/\b(\d)\b/0$1/g; print "$a --> $b\n";} snmplogs/router.txt'

Illegal division by zero at -e line 1, <> line 1.

Open in new window



And txt file :

IP-MIB::ipNetToPhysicalPhysAddress.1107755015.ipv4.172.16.1.21 = STRING: 0:18:6e:37:cf:28
IP-MIB::ipNetToPhysicalPhysAddress.1107755015.ipv4.172.16.1.22 = STRING: 0:1:e8:d6:53:37
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.10.1.1.1 = STRING: 0:1:e8:d6:53:37
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.3 = STRING: 0:50:56:be:70:d8
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.5 = STRING: 0:4:ac:e3:e8:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.6 = STRING: 0:4:ac:e3:e8:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.9 = STRING: 2:d0:68:12:4b:cc
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.18 = STRING: 90:2b:34:9d:53:cb
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.19 = STRING: 90:2b:34:9d:53:cb
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.20 = STRING: 90:2b:34:a0:42:f3
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.21 = STRING: 90:2b:34:a0:42:f3
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.34 = STRING: e0:69:95:2e:90:a4
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.35 = STRING: e0:69:95:2e:90:a4
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.36 = STRING: 90:2b:34:a0:42:f3
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.39 = STRING: e0:69:95:2e:90:a4
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.67 = STRING: b8:ac:6f:97:82:6f
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.116 = STRING: 0:4:ac:e3:e8:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.162 = STRING: 0:50:56:be:36:c1
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.178 = STRING: 0:50:56:96:50:fa
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.179 = STRING: 0:50:56:96:3:8b
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.180 = STRING: 0:50:56:96:3:c3
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.181 = STRING: 0:50:56:96:3:8b
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.182 = STRING: 0:50:56:96:3:8b
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.183 = STRING: 0:50:56:96:3:8b
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.184 = STRING: 0:50:56:96:3:c3
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.185 = STRING: 0:50:56:96:25:99
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.188 = STRING: 0:50:56:96:3:c3
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.189 = STRING: 0:50:56:96:3:8b
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.226 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.227 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.228 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.229 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.230 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.231 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.232 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.233 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.234 = STRING: 0:50:56:be:d0:49
IP-MIB::ipNetToPhysicalPhysAddress.1107787875.ipv4.37.123.96.235 = STRING: 0:50:56:be:d0:49

Open in new window

0
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
ID: 39724901
Ok, try:

perl -ne 'if(($a,$b)=m{ipv4\.(\S+)\s=\sSTRING:\s(\S+)}) { $b =~ s/\b(\d)\b/0$1/g; print "$a --> $b\n";}' snmplogs/router.txt

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
ID: 39724915
perl -ne 'if(($a,$b)=m{ipv4\.(\S+)\s=\sSTRING:\s(\S+)}) { $b =~ s/\b(\d)\b/0$1/g; print "$a --> $b\n";}' file

Open in new window


This is what I get:

172.16.1.21 --> 00:18:6e:37:cf:28
172.16.1.22 --> 00:01:e8:d6:53:37
10.1.1.1 --> 00:01:e8:d6:53:37
37.123.96.3 --> 00:50:56:be:70:d8
37.123.96.5 --> 00:04:ac:e3:e8:49
37.123.96.6 --> 00:04:ac:e3:e8:49
37.123.96.9 --> 02:d0:68:12:4b:cc
37.123.96.18 --> 90:2b:34:9d:53:cb
37.123.96.19 --> 90:2b:34:9d:53:cb
37.123.96.20 --> 90:2b:34:a0:42:f3
37.123.96.21 --> 90:2b:34:a0:42:f3
37.123.96.34 --> e0:69:95:2e:90:a4
37.123.96.35 --> e0:69:95:2e:90:a4
37.123.96.36 --> 90:2b:34:a0:42:f3
37.123.96.39 --> e0:69:95:2e:90:a4
37.123.96.67 --> b8:ac:6f:97:82:6f
37.123.96.116 --> 00:04:ac:e3:e8:49
37.123.96.162 --> 00:50:56:be:36:c1
37.123.96.178 --> 00:50:56:96:50:fa
37.123.96.179 --> 00:50:56:96:03:8b
37.123.96.180 --> 00:50:56:96:03:c3
37.123.96.181 --> 00:50:56:96:03:8b
37.123.96.182 --> 00:50:56:96:03:8b
37.123.96.183 --> 00:50:56:96:03:8b
37.123.96.184 --> 00:50:56:96:03:c3
37.123.96.185 --> 00:50:56:96:25:99
37.123.96.188 --> 00:50:56:96:03:c3
37.123.96.189 --> 00:50:56:96:03:8b
37.123.96.226 --> 00:50:56:be:d0:49
37.123.96.227 --> 00:50:56:be:d0:49
37.123.96.228 --> 00:50:56:be:d0:49
37.123.96.229 --> 00:50:56:be:d0:49
37.123.96.230 --> 00:50:56:be:d0:49
37.123.96.231 --> 00:50:56:be:d0:49
37.123.96.232 --> 00:50:56:be:d0:49
37.123.96.233 --> 00:50:56:be:d0:49
37.123.96.234 --> 00:50:56:be:d0:49
37.123.96.235 --> 00:50:56:be:d0:49

Open in new window

0
 

Author Closing Comment

by:3XLcom
ID: 39724918
That is it thanks .
0ne last question is there any wat the make all chars upper case
0
 
LVL 31

Expert Comment

by:farzanj
ID: 39724940
Welcome.

Try this:
perl -ne 'if(($a,$b)=m{ipv4\.(\S+)\s=\sSTRING:\s(\S+)}) { $b =~ s/\b(\d)\b/0$1/g; print "$a --> ".uc($b)."\n";}' file

Open in new window

0
 

Author Comment

by:3XLcom
ID: 39724949
perfect thnx
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question