LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-06-2011, 09:27 AM   #1
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Rep: Reputation: 16
Question 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.
 
Old 04-06-2011, 09:58 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.
Old 04-06-2011, 10:04 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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?
 
Old 04-06-2011, 11:28 AM   #4
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
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 ...
 
Old 04-06-2011, 11:38 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
Old 04-06-2011, 12:39 PM   #6
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Thanks Garil,

Once Again Short and Sweet...!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Substitue single-digit, two-digit, and 3-digit numbers with text using sed dmason165 Programming 13 08-07-2009 10:38 AM
cd to a directory name - (minus sign) anjanesh Linux - General 4 12-29-2008 03:38 AM
/etc/shadow- (with minus) twk Linux - Security 4 06-19-2007 06:58 PM
mepis minus mc kim wally Linux - Software 1 08-14-2006 07:27 PM
lilo minus v jamestwok Linux - General 3 10-22-2002 08:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration