how to change last value (ip address) with sed
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I read the current ip address with following command line. after then i need to replace the last value with 0/24
For example. The current ip ist 192.168.178.1 and i need to replace the var value in 192.168.178.0/24
Thank you for your support and best greetings!
varip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' )
shell-script awk sed ip replace
add a comment |Â
up vote
0
down vote
favorite
I read the current ip address with following command line. after then i need to replace the last value with 0/24
For example. The current ip ist 192.168.178.1 and i need to replace the var value in 192.168.178.0/24
Thank you for your support and best greetings!
varip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' )
shell-script awk sed ip replace
Maybe this is easier to parse:hostname -i | sed 's/[^.]*$/0/24/'
â Valentin B
Oct 9 '17 at 19:55
If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
â Kusalananda
Oct 12 '17 at 19:34
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I read the current ip address with following command line. after then i need to replace the last value with 0/24
For example. The current ip ist 192.168.178.1 and i need to replace the var value in 192.168.178.0/24
Thank you for your support and best greetings!
varip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' )
shell-script awk sed ip replace
I read the current ip address with following command line. after then i need to replace the last value with 0/24
For example. The current ip ist 192.168.178.1 and i need to replace the var value in 192.168.178.0/24
Thank you for your support and best greetings!
varip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' )
shell-script awk sed ip replace
shell-script awk sed ip replace
asked Oct 9 '17 at 19:32
mikeeyy
1
1
Maybe this is easier to parse:hostname -i | sed 's/[^.]*$/0/24/'
â Valentin B
Oct 9 '17 at 19:55
If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
â Kusalananda
Oct 12 '17 at 19:34
add a comment |Â
Maybe this is easier to parse:hostname -i | sed 's/[^.]*$/0/24/'
â Valentin B
Oct 9 '17 at 19:55
If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
â Kusalananda
Oct 12 '17 at 19:34
Maybe this is easier to parse:
hostname -i | sed 's/[^.]*$/0/24/'
â Valentin B
Oct 9 '17 at 19:55
Maybe this is easier to parse:
hostname -i | sed 's/[^.]*$/0/24/'
â Valentin B
Oct 9 '17 at 19:55
If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
â Kusalananda
Oct 12 '17 at 19:34
If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
â Kusalananda
Oct 12 '17 at 19:34
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
1
down vote
Use the following sed code at the end:
sed 's:[^.]*$:0/24:'
It works by replacing the last occurring sub-string that doesn't contain '.', with '0/24'. Note how I use ':' as separator for the sed command s
, so that I can use '/' un-escaped.
Or go with sed all the way :)
ip addr | sed -rn '/state UP/n;n;s:^ *[^ ]* *([^ ]*).*:1:;s:[^.]*$:0/24:p'
add a comment |Â
up vote
1
down vote
Try this:
ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' | sed 's/.[0-9]*$/.0/24/'
But you do not need cut
, replace it with sed
:
ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | sed 's/.[0-9]*//.0//'
This command return the same output.
And you do not need tail
, replace it with sed
too:
ip addr | grep 'state UP' -A2 | awk 'print $2' | sed '$!d;s/.[0-9]*//.0//'
But the shortest way is to use hostname
instead:
hostname -I | tr -d " " | sed 's/[0-9]*$/0/24/'
Output will be the same.
add a comment |Â
up vote
0
down vote
To get Your-IP-Adress/24 use the following command:
ip addr | grep inet | egrep /24 | awk 'print $2'
add a comment |Â
up vote
0
down vote
Here is a perl
one liner:
perl -MSys::Hostname -MEnv -e ' print join ".",( unpack("C4",(gethostbyname($HOSTNAME))[4]))[0..2],"0/24n";'
It is predicated on the HOSTNAME environmental variable being set to the proper machine name.
add a comment |Â
up vote
0
down vote
Just using Awk:
ip a | awk '/state UP/nr[NR+2]; NR in nr gsub(/.([0-9]+)/([0-9]+)/,".0/24"); print $2'
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Use the following sed code at the end:
sed 's:[^.]*$:0/24:'
It works by replacing the last occurring sub-string that doesn't contain '.', with '0/24'. Note how I use ':' as separator for the sed command s
, so that I can use '/' un-escaped.
Or go with sed all the way :)
ip addr | sed -rn '/state UP/n;n;s:^ *[^ ]* *([^ ]*).*:1:;s:[^.]*$:0/24:p'
add a comment |Â
up vote
1
down vote
Use the following sed code at the end:
sed 's:[^.]*$:0/24:'
It works by replacing the last occurring sub-string that doesn't contain '.', with '0/24'. Note how I use ':' as separator for the sed command s
, so that I can use '/' un-escaped.
Or go with sed all the way :)
ip addr | sed -rn '/state UP/n;n;s:^ *[^ ]* *([^ ]*).*:1:;s:[^.]*$:0/24:p'
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Use the following sed code at the end:
sed 's:[^.]*$:0/24:'
It works by replacing the last occurring sub-string that doesn't contain '.', with '0/24'. Note how I use ':' as separator for the sed command s
, so that I can use '/' un-escaped.
Or go with sed all the way :)
ip addr | sed -rn '/state UP/n;n;s:^ *[^ ]* *([^ ]*).*:1:;s:[^.]*$:0/24:p'
Use the following sed code at the end:
sed 's:[^.]*$:0/24:'
It works by replacing the last occurring sub-string that doesn't contain '.', with '0/24'. Note how I use ':' as separator for the sed command s
, so that I can use '/' un-escaped.
Or go with sed all the way :)
ip addr | sed -rn '/state UP/n;n;s:^ *[^ ]* *([^ ]*).*:1:;s:[^.]*$:0/24:p'
edited Oct 9 '17 at 20:14
answered Oct 9 '17 at 19:49
seshoumara
25127
25127
add a comment |Â
add a comment |Â
up vote
1
down vote
Try this:
ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' | sed 's/.[0-9]*$/.0/24/'
But you do not need cut
, replace it with sed
:
ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | sed 's/.[0-9]*//.0//'
This command return the same output.
And you do not need tail
, replace it with sed
too:
ip addr | grep 'state UP' -A2 | awk 'print $2' | sed '$!d;s/.[0-9]*//.0//'
But the shortest way is to use hostname
instead:
hostname -I | tr -d " " | sed 's/[0-9]*$/0/24/'
Output will be the same.
add a comment |Â
up vote
1
down vote
Try this:
ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' | sed 's/.[0-9]*$/.0/24/'
But you do not need cut
, replace it with sed
:
ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | sed 's/.[0-9]*//.0//'
This command return the same output.
And you do not need tail
, replace it with sed
too:
ip addr | grep 'state UP' -A2 | awk 'print $2' | sed '$!d;s/.[0-9]*//.0//'
But the shortest way is to use hostname
instead:
hostname -I | tr -d " " | sed 's/[0-9]*$/0/24/'
Output will be the same.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Try this:
ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' | sed 's/.[0-9]*$/.0/24/'
But you do not need cut
, replace it with sed
:
ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | sed 's/.[0-9]*//.0//'
This command return the same output.
And you do not need tail
, replace it with sed
too:
ip addr | grep 'state UP' -A2 | awk 'print $2' | sed '$!d;s/.[0-9]*//.0//'
But the shortest way is to use hostname
instead:
hostname -I | tr -d " " | sed 's/[0-9]*$/0/24/'
Output will be the same.
Try this:
ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' | sed 's/.[0-9]*$/.0/24/'
But you do not need cut
, replace it with sed
:
ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | sed 's/.[0-9]*//.0//'
This command return the same output.
And you do not need tail
, replace it with sed
too:
ip addr | grep 'state UP' -A2 | awk 'print $2' | sed '$!d;s/.[0-9]*//.0//'
But the shortest way is to use hostname
instead:
hostname -I | tr -d " " | sed 's/[0-9]*$/0/24/'
Output will be the same.
edited Oct 9 '17 at 20:18
answered Oct 9 '17 at 19:46
Egor Vasilyev
1,792129
1,792129
add a comment |Â
add a comment |Â
up vote
0
down vote
To get Your-IP-Adress/24 use the following command:
ip addr | grep inet | egrep /24 | awk 'print $2'
add a comment |Â
up vote
0
down vote
To get Your-IP-Adress/24 use the following command:
ip addr | grep inet | egrep /24 | awk 'print $2'
add a comment |Â
up vote
0
down vote
up vote
0
down vote
To get Your-IP-Adress/24 use the following command:
ip addr | grep inet | egrep /24 | awk 'print $2'
To get Your-IP-Adress/24 use the following command:
ip addr | grep inet | egrep /24 | awk 'print $2'
answered Oct 9 '17 at 20:07
GAD3R
22.7k154895
22.7k154895
add a comment |Â
add a comment |Â
up vote
0
down vote
Here is a perl
one liner:
perl -MSys::Hostname -MEnv -e ' print join ".",( unpack("C4",(gethostbyname($HOSTNAME))[4]))[0..2],"0/24n";'
It is predicated on the HOSTNAME environmental variable being set to the proper machine name.
add a comment |Â
up vote
0
down vote
Here is a perl
one liner:
perl -MSys::Hostname -MEnv -e ' print join ".",( unpack("C4",(gethostbyname($HOSTNAME))[4]))[0..2],"0/24n";'
It is predicated on the HOSTNAME environmental variable being set to the proper machine name.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Here is a perl
one liner:
perl -MSys::Hostname -MEnv -e ' print join ".",( unpack("C4",(gethostbyname($HOSTNAME))[4]))[0..2],"0/24n";'
It is predicated on the HOSTNAME environmental variable being set to the proper machine name.
Here is a perl
one liner:
perl -MSys::Hostname -MEnv -e ' print join ".",( unpack("C4",(gethostbyname($HOSTNAME))[4]))[0..2],"0/24n";'
It is predicated on the HOSTNAME environmental variable being set to the proper machine name.
edited Oct 9 '17 at 20:13
GAD3R
22.7k154895
22.7k154895
answered Oct 9 '17 at 19:49
DannyK
20413
20413
add a comment |Â
add a comment |Â
up vote
0
down vote
Just using Awk:
ip a | awk '/state UP/nr[NR+2]; NR in nr gsub(/.([0-9]+)/([0-9]+)/,".0/24"); print $2'
add a comment |Â
up vote
0
down vote
Just using Awk:
ip a | awk '/state UP/nr[NR+2]; NR in nr gsub(/.([0-9]+)/([0-9]+)/,".0/24"); print $2'
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Just using Awk:
ip a | awk '/state UP/nr[NR+2]; NR in nr gsub(/.([0-9]+)/([0-9]+)/,".0/24"); print $2'
Just using Awk:
ip a | awk '/state UP/nr[NR+2]; NR in nr gsub(/.([0-9]+)/([0-9]+)/,".0/24"); print $2'
answered Oct 9 '17 at 23:36
jasonwryan
47.1k14127178
47.1k14127178
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f397107%2fhow-to-change-last-value-ip-address-with-sed%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Maybe this is easier to parse:
hostname -i | sed 's/[^.]*$/0/24/'
â Valentin B
Oct 9 '17 at 19:55
If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
â Kusalananda
Oct 12 '17 at 19:34