Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert ip

Status
Not open for further replies.

LeHibou

Technical User
Jun 12, 2013
3
0
0
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,

 
Hi

LeHibou said:
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:
[COLOR=darkgoldenrod]function ip2dec[/color][teal]([/teal]ip[teal])[/teal]
[teal]{[/teal]
  [b]split[/b][teal]([/teal][navy]$1[/navy][teal],[/teal]n[teal],[/teal][fuchsia]/\./[/fuchsia][teal])[/teal]
  [navy]d[/navy][teal]=[/teal][purple]0[/purple]
  [b]for[/b] [teal]([/teal][navy]i[/navy][teal]=[/teal][purple]1[/purple][teal];[/teal]i[teal]<=[/teal][purple]4[/purple][teal];[/teal]i[teal]++)[/teal] d[teal]+=[/teal]n[teal][[/teal]i[teal]]*[/teal][purple]256[/purple][teal]**([/teal][purple]4[/purple]-i[teal])[/teal]
  [b]return[/b] d
[teal]}[/teal]
Note that the above function works well in [tt]gawk[/tt] but not in [tt]mawk[/tt].

Feherke.
feherke.github.io
 
Many thanks,

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

You rock !

LeHibou
 
Hi

Oops, a bug. Intended to process the function's parameter, not the input field :
Code:
[b]split[/b][teal]([/teal][highlight]ip[/highlight][teal],[/teal]n[teal],[/teal][fuchsia]/\./[/fuchsia][teal])[/teal]


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top