decrement a four part number in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting decrement a four part number in shell script
# 1  
Old 12-29-2009
decrement a four part number in shell script

I have a four part number

eg: 1.21.1.3

I need to find a way in shell script to decrement this by one and put in a loop

so the values printed will be

1.21.1.2
1.21.1.1
1.21.1.0

Which is the best way to do this in shell script??
# 2  
Old 12-29-2009
Code as below...you can let x be read by while loop from a file.


Code:
x=1.21.1.4
y=${x##[0-9]*.}
while  [[ ${y} -ge  0 ]]
do
echo ${x%.[0-9]*}.${y}
(( y-- ))
done

Code:
o/p:-
1.21.1.4
1.21.1.3
1.21.1.2
1.21.1.1
1.21.1.0

SmilieSmilieSmilieSmilieSmilie

---------- Post updated at 10:13 ---------- Previous update was at 10:01 ----------

I mean in above that if you have
a file like below:-


Code:
cat file.txt:-
1.2.3.4
2.3.4.5
3.4.5.6
.
.
.


Just add another while loop to read
the value of x :-

New code:-


Code:
while read x
do
y=${x##[0-9]*.}
while  [[ ${y} -ge  0 ]]
do
echo "${x%.[0-9]*}"".""${y}"
(( y-- ))
done
done < file.txt



Code:
o/p:-
1.2.3.4
1.2.3.3
1.2.3.2
1.2.3.1
1.2.3.0
2.3.4.5
2.3.4.4
2.3.4.3
2.3.4.2
2.3.4.1
2.3.4.0
3.4.5.6
3.4.5.5
3.4.5.4
3.4.5.3
3.4.5.2
3.4.5.1
3.4.5.0


SmilieSmilieSmilieSmilie
# 3  
Old 12-29-2009
awk 'BEGIN{FS=".";} {if($4 >0){ while(x>=0) {x=$4--;print $1"."$2"."$3"."x;}}' filename

#filename has the IPs to be printed
# 4  
Old 12-29-2009
Quote:
Originally Posted by ahmad.diab
Code as below...you can let x be read by while loop from a file.

Code:
x=1.21.1.4
y=${x##[0-9]*.}
while  [[ ${y} -ge  0 ]]
do
echo ${x%.[0-9]*}.${y}
(( y-- ))
done

Hi Ahmad,

Your code works well, but you do not need the [0-9] and if you change the (( y-- )) and the [[ ]] it will be posix compliant:

Code:
x=10.121.123.114
y=${x##*.}
while [ $y -ge 0 ]
do
  echo ${x%.*}.${y}
  y=$(( y-1 ))
done

# 5  
Old 12-29-2009
Quote:
Originally Posted by Scrutinizer
Hi Ahmad,

Your code works well, but you do not need the [0-9] and if you change the (( y-- )) and the [[ ]] it will be posix compliant:

Code:
x=10.121.123.114
y=${x##*.}
while [ $y -ge 0 ]
do
  echo ${x%.*}.${y}
  y=$(( y-1 ))
done


Np problem man this ([[ & (())) is because I am using bash...but ${x##[0-9]*.} is just an easy way to insure that the input is a number
else it will give an error.
# 6  
Old 12-29-2009
Hi Ahmad, that is the thing. If your last number is e.g. a letter your code will not raise an error, whereas if you leave out the [0-9] it will.
If I feed e.g.:
Code:
x=10.123.4.A

the output will be :
Code:
10.123.A

Whereas if we leave out the [0-9]:
Code:
./test: line 3: [: a: integer expression expected

which is what you want. The pattern operator will not raise the error, but the test will.

Last edited by Scrutinizer; 12-29-2009 at 06:03 AM..
# 7  
Old 12-29-2009
No problem man...SmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substr/Instr in shell script or extract part of text

Hi, I need to extract part of a text to two variables text is "PL/SQL procedure successfully completed. ERROR ----------------------------------------------------------------- Test Error Message PLUSVAR ---------- 1" I want "Test Error Message" in one variable and "1" in another variable.... (11 Replies)
Discussion started by: vedavrath
11 Replies

2. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

3. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

4. Shell Programming and Scripting

Shell script - Replace just part of a single line in a file.....

Hey guy's.... I new here, But im working on a school project, and I am not really good at programming. In fact, this is the only programming class that I need because programming is not what I am majoring in. But I have everything done in this shell script except for this last part..... ... (9 Replies)
Discussion started by: hxdrummerxc
9 Replies

5. Shell Programming and Scripting

Part of the Shell script is not running via crontab, runs fine manually

Hello Team, As a part of my job we have made a script to automate a service to restart frequently. Script having two functions when executing it's should find the existing service and kill it, then start the same service . Verified the script it's working fine when executing... (18 Replies)
Discussion started by: gowthamakanthan
18 Replies

6. Shell Programming and Scripting

Decrement one from 3rd Column

Hi, I need a script that will subtract 1 from the third column of the line beginning with %, leaving all other values the same. So 158 should be 157, 308 should be 307, 458 should be 457. Before: # 30109 xyz abc Data % 30109 158 5 8 2 000023f 01f4145 # 30109 ... (3 Replies)
Discussion started by: morrbie
3 Replies

7. Shell Programming and Scripting

Decrement using bash!!

Hi all, Thanks in Advance! I want a simple script to print today and yesterdays date. using this command date +%d%m%Y i can able get today's date but i want yesterday's date with the same format. so i tried using simple decrement operator but... (2 Replies)
Discussion started by: anishkumarv
2 Replies

8. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

9. Shell Programming and Scripting

Can we increment or decrement a date value?

export a=`date` a=`expr $a + 1` Is it possible? if not how can i increment or decrement a date variable? (2 Replies)
Discussion started by: arghya_owen
2 Replies

10. Shell Programming and Scripting

Execute a part of shell script only after particular date and time

I have created a simple shell script... say test.sh Contents of test.sh ================ service named restart cp /etc/imp.conf /backup/test/ #-- if date > 15 July 2007 11:23 pm , then only issue the commans below, else exit --- cp /etc/secondimp.conf /backup/test/ rm -f... (2 Replies)
Discussion started by: fed.linuxgossip
2 Replies
Login or Register to Ask a Question