localip sed illegal option
tnguyen14 opened this issue · comments
I tried to use localip
function, but getting this error. Do you know what I might be missing here?
:; localip
sed: illegal option -- r
usage: sed script [-Ealn] [-i extension] [file ...]
sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
sed: illegal option -- r
usage: sed script [-Ealn] [-i extension] [file ...]
sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
sed: illegal option -- r
usage: sed script [-Ealn] [-i extension] [file ...]
sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
sed: illegal option -- r
usage: sed script [-Ealn] [-i extension] [file ...]
sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
probably need GNU sed. :/
brew install gnu-sed --with-default-names
i think
I think I already have that installed
:; brew install gnu-sed --with-default-names
Warning: gnu-sed-4.2.2 already installed
@tnguyen14 you have it installed but without default names.
do brew rm gnu-sed
to uninstall and try again
also if you're really feeling up to it you can fix this function so it works with BSD sed..
but.. no worries if you're not. :)
So I tried to reinstall gnu-sed
, but brew kept error-ing out on me.
Rewriting that with BSD sed was really difficult, the most of which was the e
flag on this command sed -r "s/Device: (en.*)$/_localip \1/e"
. I can't seem to find the equivalent for BSD sed.
I ended up stumbling upon this answer http://stackoverflow.com/a/33550399/1368032 and copied it to have the following function
function localip(){
local if=$(netstat -rn | awk '/^0.0.0.0/ {thif=substr($0,74,10); print thif;} /^default.*UG/ {thif=substr($0,65,10); print thif;}')
local ip=$(ifconfig ${if} | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
echo $ip
}
it seems to be working for me.
It is not quite the full replacement though as it only returns the ip of the default interface (en0
), and not all of them.
tbh ive never used that function. so again.. very delayed.. but i've just removed it :). 3dffc6e