LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 12-01-2011, 09:26 AM   #1
drwatson_droid
LQ Newbie
 
Registered: Jun 2011
Location: India
Distribution: Ubuntu
Posts: 3

Rep: Reputation: Disabled
Smile Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008)


Hi,
I have the following requirement.

There will be following text/line in a file (eg: search-build.txt)
PRODUCT_VERSION="V:01.002.007.Build1234"

I need to update the incremental build number (eg here 007) every time I give a build through script. I am able to search the string and get the item.
Quote:
rexpression_sed='PRODUCT_VERSION=\"V:\([0-9]*\).\([0-9]*\).\([0-9]*\).Build\([0-9]*\)\"'
var2=`grep -sn $rexpression_sed search-build.txt | sed -e 's/'$rexpression_sed'/\1\t\2\t\3\t\4/g'| cut -f 3`
echo $var2
Output
007

How to convert this to integer (7), add 1 to it (8) and again get back in the same form (008) in the easiest way? I will be using sed -i on the file, with this new string that can be used with RE for replacing that part!
Or
Is there any better method than above.

Thanks for any help.

Last edited by drwatson_droid; 12-01-2011 at 09:29 AM.
 
Old 12-01-2011, 09:38 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
If you add the following to the end of your script you'll get the increment:
Code:
calcval=$(expr $var2 + 1)
echo $calcval
newval="00$calcval"
echo $newval
You'd then have to plug $newval into the rest of the string.
 
1 members found this post helpful.
Old 12-01-2011, 09:40 AM   #3
drwatson_droid
LQ Newbie
 
Registered: Jun 2011
Location: India
Distribution: Ubuntu
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MensaWater View Post
If you add the following to the end of your script you'll get the increment:
Code:
calcval=$(expr $var2 + 1)
echo $calcval
newval="00$calcval"
echo $newval
You'd then have to plug $newval into the rest of the string.
thanks a lot
 
Old 12-01-2011, 09:42 AM   #4
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
Is the string always supposed to be 3 digits, or have 2 leading zeroes (would "10" be 010 or 0010)?
 
1 members found this post helpful.
Old 12-01-2011, 10:24 AM   #5
drwatson_droid
LQ Newbie
 
Registered: Jun 2011
Location: India
Distribution: Ubuntu
Posts: 3

Original Poster
Rep: Reputation: Disabled
thanks. solved. final script is here

here is the final script
Quote:
newval=0
filename=search-build.txt
rexpression_sed='PRODUCT_VERSION=\"V:\([0-9]*\).\([0-9]*\).\([0-9]*\).Build\([0-9]*\)\"'

incrementnum=`grep -sn $rexpression_sed $filename | sed -e 's/'$rexpression_sed'/\1\t\2\t\3\t\4/g'| cut -f 3`
echo "Current Incremental number is: " $incrementnum
if [ $incrementnum ]
then
calcval=$(expr $incrementnum + 1)
#echo $calcval
if [ $calcval -le 9 ];
then
newval="00$calcval"
elif [ $calcval -le 99 ];
then
newval="0$calcval"
else
newval="$calcval"
fi
echo "New Incremental number is: " $newval
rexpression_rep_final='PRODUCT_VERSION="V:\1.\2.'$newval'.Build\4"'
#replace with new number
sed -i 's/'$rexpression_sed'/'$rexpression_rep_final'/' $filename
fi

Last edited by drwatson_droid; 12-01-2011 at 10:25 AM.
 
Old 12-01-2011, 10:44 AM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Use `printf' to pad your number with zeros.
For example:
Code:
$ printf "%0.3d\n" 15
015
'0.3d' means print an integer with minimum width of 3 digits, padding it with zeros if necessary. See `man printf' and `man 3 printf' for details.

Last edited by firstfire; 12-01-2011 at 10:45 AM. Reason: Fix a typo.
 
2 members found this post helpful.
  


Reply



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
convert string to int testac Programming 8 07-06-2010 10:12 PM
[Bash] increment number in a string czezz Programming 4 07-01-2009 11:34 AM
convert int to string mohtasham1983 Programming 15 03-02-2007 06:18 PM
Convert string (C++) to int FreeDoughnut Programming 10 10-30-2006 03:26 PM
convert string to long/int alaios Programming 10 09-15-2005 09:01 AM

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

All times are GMT -5. The time now is 10:24 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