Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String substitution in shell script 1

Status
Not open for further replies.

kobewins

Programmer
Dec 1, 2005
57
0
0
US
I have two shell variables(v1, v3). I want to substitue a part of one variable (v1) with the other(v3).

let v1=NPDL0000000
let v2=12
let des=

v3=`expr $v2 + 1`

I would like des=NPDL0000013, which comes from

NPDL0000000 (v1)
13 (v3)
---------------------------
NPDL0000013 (des)

I know I may be able to do this by awk ( on the file base).
But is there a way to do it in shell script?

Any help is greatly appreciated.
 
Hi

I think I do not understand exactly, but maybe :
Code:
[blue]master #[/blue] v1=NPDL0000000
[blue]master #[/blue] v2=12
[blue]master #[/blue] v3=${v1%??}$v2
[blue]master #[/blue] echo $v3
NPDL0000012

Feherke.
 
Now I got aonther problem.

v2 could be 1 to 9999999, how does the program know how many question marks to put in

v3=${v1%???}$2 ## Here v2 is a 3-digit number: v2=123
v3=${v1%??}$2 ## Here v2 is a 2-digit number: v2=12
v3=${v1%?}$2 ## Here v2 is a 1-digit number: v2=5

etc.


 
Try this...
Code:
$ v1=NPDL0000000
$ typeset -Z7 v2=12
$ v3=${v1%???????}$v2
$ echo $v3
NPDL0000012
The typeset makes [tt]v2[/tt] always be seven digits zero filled, so you just chop all the digits off and append [tt]v2[/tt].
 
I guess you wanted this ?
v1=NPDL0000000
v2=12
typeset -Z7 v3=$((v2+1))
des=${v1%???????}$v3
echo $des

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top