INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Convert ip

Convert ip

(OP)
Hello,

I currently have this script :

CODE

awk 'BEGIN{OFS=",";} {print substr($4,2)}' my.log > log.txt 

I simplified it since it would normally be :

CODE

awk 'BEGIN{OFS=",";} {print substr($4,2),$1,$6,$8}' my.log > log.txt 

I would like to be able to convert an ip (the $4 field) 192.168.1.1 to its decimal counterpart.


So, i shoud do something around substr($4,2) since it it the column i am interested in.

Could someone tell me how to do so ?

Thanks,

RE: Convert ip

Hi

Quote (LeHibou)

I would like to be able to convert an ip (the $4 field) 192.168.1.1 to its decimal counterpart.
Write a function to do the conversion then use it ?

CODE --> GNU Awk

function ip2dec(ip)
{
  split($1,n,/\./)
  d=0
  for (i=1;i<=4;i++) d+=n[i]*256**(4-i)
  return d
} 
Note that the above function works well in gawk but not in mawk.

Feherke.
feherke.github.io

RE: Convert ip

(OP)
Many thanks,

totally missed the -f option to put the function file to launch :)

You rock !

LeHibou

RE: Convert ip

Hi

Oops, a bug. Intended to process the function's parameter, not the input field :

CODE

split(ip,n,/\./) 

Feherke.
feherke.github.io

RE: Convert ip

(OP)
Yes ! I corrected this :)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close