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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008)
# 1  
Old 12-01-2011
Bug 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.
# 2  
Old 12-01-2011
Code:
$ PRODUCT_VERSION="V:01.002.007.Build1234"
$ printf "%03d" $(expr $(echo $PRODUCT_VERSION | cut -d. -f3) + 1)
008

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 12-01-2011
Code:
echo V:01.002.007.Build1234 | awk -F . '{print "00"$3+1}'

This User Gave Thanks to funksen For This Post:
# 4  
Old 12-01-2011
thanks a lot

---------- Post updated at 09:56 PM ---------- Previous update was at 09:49 PM ----------

Re: Bash shell script: Str(007) to int(7),increment it(Image & convert back to string(00
thanks solved
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
# 5  
Old 12-01-2011
Quote:
Originally Posted by balajesuri
Code:
$ PRODUCT_VERSION="V:01.002.007.Build1234"
$ printf "%03d" $(expr $(echo $PRODUCT_VERSION | cut -d. -f3) + 1)
008

In modern versions of bash you do not need to use expr, i.e.
Code:
 printf "%03d" $(( $(echo $PRODUCT_VERSION | cut -d. -f3) + 1))

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script problems int to binary

Hi, I am trying to do a bash script that convert a decimal number to a binary value, but it doesn't work... To begin, I am just trying to convert a positive number to 8 bits binary. read -p"Entrez un nombre entre -128 et 127 pour l'encoder en binaire: " number binaryValues=(128 64 32 16 8 4 2... (8 Replies)
Discussion started by: Zedki
8 Replies

2. Shell Programming and Scripting

Convert int to string in python

Hi, I have column 5 in a file which contains string like this for ex. RP11-125O5.2 SLCO1B1 CAPN1 FRMPD2 TXNL4B So I do by data = )] ValueError: invalid literal for int() with base 10: 'R' Can someday tell me how to convert this column into int successfully. Thank You in... (7 Replies)
Discussion started by: rossi
7 Replies

3. Shell Programming and Scripting

Convert a string to variable in Bash shell

Hi All; I have 2 variable let's say $A and $B. These are actually some remotely executed command outputs. I captured these values in my local variables. Here is my problem. When I try to do some arithmetic operation with these variables, I receive an error message. Neither expr nor typeset -i... (3 Replies)
Discussion started by: Meacham12
3 Replies

4. Shell Programming and Scripting

Problem in Concatination of string in bash scripts containing back slashes.

My script is as follows: #!/bin/bash STR1="test" echo $STR1 STR2="/bldtmp/"$STR1 echo $STR2 STR3=$STR2'/tmp' echo $STR3 output i am geting ---------------- test /bldtmp/test /tmptmp/test but my need is: ------------------ test /bldtmp/test (1 Reply)
Discussion started by: dchoudhury
1 Replies

5. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

6. UNIX for Dummies Questions & Answers

bash script to increment a digit in filename

Hi guys, Can someone help me out with this: I have a directory with files like the following, GHost++ 2010-03-14 04-01 DotaCash RD us_ca LC #7 (44m19s).w3g GHost++ 2010-03-14 04-06 DotaCash AP us_ca LC #8 (42m24s).w3g GHost++ 2010-03-14 04-07 DotaCash AR us_ca LC #10 (08m23s).w3g ... (4 Replies)
Discussion started by: hbjlee17
4 Replies

7. Shell Programming and Scripting

bash shell script string comparison

I want to remove a line that has empty string at second field when I use cut with delimeter , like below $cat demo hello, mum hello, #!/bin/sh while read line do if then # remove the current line command goes here fi done < "demo" i got an error message for above... (4 Replies)
Discussion started by: bonosungho
4 Replies

8. Shell Programming and Scripting

Interesting TCL behavior: 007 == 7 is true; 008==8 is false.

Hi all, If anyone has the explanation for the following issue, please share it with me. I am comparing two variable a and b with the values of 007 and 7, for these values it get evaluated as True. For a=008 and b=8, for these values it get evaluated as false. #!/bin/tclsh set a 007 ... (3 Replies)
Discussion started by: sarwan
3 Replies

9. Shell Programming and Scripting

how to convert a string to int

Hi, i want to read a text file whose content(single line) will be a number like 1 or 2 or 3 ..... what i want to do is to read the file and increment the content of the file, using unix scripting. Regards, Senthil Kumar Siddhan. (2 Replies)
Discussion started by: senthilk615
2 Replies

10. Programming

Howto convert Ascii -> UTF-8 & back C++

While working with russian text under FreeBSD&MySQL I need to convert a string from MySQL to the Unicode format. I've just started my way in C++ under FreeBSD , so please explain me how can I get ascii code of Char variable and also how can i get a character into variable with the specified ascii... (3 Replies)
Discussion started by: macron
3 Replies
Login or Register to Ask a Question