Register
It is currently Mon Apr 23, 2018 6:52 pm

invert IP vs MAC


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed Nov 15, 2017 3:19 pm   

Joined: Thu Nov 02, 2017 7:43 am
Posts: 3
Hy every body,

let say we got:
Code:
192.168.0.1  xx:xx:xx:xx:xx:xx
192.168.0.2 yy:yy:yy:yy:yy:yy
192.168.0.3 zz:zz:zz:zz:zz:zz


Using the sed command, how to invert them like this :

Code:
xx:xx:xx:xx:xx:xx 192.168.0.1
yy:yy:yy:yy:yy:yy 192.168.0.2
zz:zz:zz:zz:zz:zz 192.168.0.3


Thank you very much for your help -:)


Top
 Profile  
 PostPosted: Thu Nov 16, 2017 4:54 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Code:
sed -rn 's/([^[:blank:]]+)[[:blank:]]+([^[:blank:]]+)/\2 \1/p' path/to/your/datafile

# or using a pipe
cat path/to/your/datafile | sed -rn 's/([^[:blank:]]+)[[:blank:]]+([^[:blank:]]+)/\2 \1/p'


Using substitute/regex/replacement/print statement.

The first group () searches for a single [ non ^ characterclass [:blank:] ] which must occur + at least one times.
Then there must be at least one char of the class [ named [:blank:] which must occur again + at least one times.
Followed again by the first part to denote the ip address as well, but this time the chars end up in the second group.

The replacement then is just group \2 followed by a blank followed by group \1


Top
 Profile  
 PostPosted: Thu Nov 16, 2017 6:08 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Note!
This "solution" works with all kinds of chars.
A file containing
Code:
one two

will print
Code:
two one


One can specify the mac and ip addresses properly using the char classe [:hdigit:] and [:digit:]
Wrote this in another post recently.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP