Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-06-2011, 09:27 AM
|
#1
|
Member
Registered: Mar 2010
Posts: 134
Rep:
|
Last digit minus ONE...
Hi All,
I am about configuring IP addresses for different workstations. in this process, after running few bash scripts to get the right subnet mask, i need to remove one digit from the last IP value, what i mean is:
ex, if i get ip=192.168.0. 254 i need 192.168.0. 253 so whatever Ip address i get i need to minus only one digit from the last IP addresswhatever it is. What is the best way to do this....
Code:
$ip | cut -d"." -f4......
or $ip | awk -F"." ;'{print $4}'...
Thank you for your help.
|
|
|
04-06-2011, 09:58 AM
|
#2
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
I'd say "Curtain B". You might be able to do something like this (I haven't tested):
Code:
$ip | awk -F"." ;'{printf "%d.%d.%d.%d", $1, $2, $3, $4 - 1}'
|
|
1 members found this post helpful.
|
04-06-2011, 10:04 AM
|
#3
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,466
|
Hi.
I agree, awk is better since it can do simple math.
Code:
echo $ip | awk -F"." '{printf "%d.%d.%d.%d", $1, $2, $3, $4 - 1}'
But what if the last digit is 0?
|
|
|
04-06-2011, 11:28 AM
|
#4
|
Member
Registered: Mar 2010
Posts: 134
Original Poster
Rep:
|
Thanks for the reply. it works as expected but yup how about the last 0 digit, i guess must test the IP address before running the code so if has 0 at the end ignores the IP address ...
|
|
|
04-06-2011, 11:38 AM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
Well it depends on whether you want go all bash or use outside commands:
Code:
ip='192.168.0.254'
#help from awk
echo $ip | awk -F. '$4=$4-1' OFS="."
# all bash
last=${ip##*.}
(( last-- ))
echo ${ip%.*}.$last
Of course none of this covers the zero case, but you may also wish to ask do you really want to ignore it or maybe set it to 255 and take one from the next octet.
Of course this would require a lot more testing and work.
|
|
1 members found this post helpful.
|
04-06-2011, 12:39 PM
|
#6
|
Member
Registered: Mar 2010
Posts: 134
Original Poster
Rep:
|
Thanks Garil,
Once Again Short and Sweet...!!
|
|
|
All times are GMT -5. The time now is 03:44 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|