Substitution within string command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substitution within string command
# 1  
Old 02-26-2014
Substitution within string command

I have the following code:
Code:
 strfuture=abcdefghi
ver=${strfuture:${count}:1}
          mj7777_ver=${ver} start_mj7777_iteration
          let count=count+1

When it is executed I get bad substitution. The same if I use
Code:
 ver=${strfuture:$count:1}
          mj7777_ver=${ver} start_mj7777_iteration

How would I do the above, please?
I am trying to do ${strfuture:1:1} ... then ${strfuture:2:1} i.e. indexed string within a loop so I do not have to write it out fully.

Is it possible?

Last edited by Scrutinizer; 02-26-2014 at 08:17 AM.. Reason: icode tags changed to code tags
# 2  
Old 02-26-2014
Which shell are you using?
# 3  
Old 02-26-2014
Hi Subbeh,
Bash shell I believe
# 4  
Old 02-26-2014
I don't know exactly what you're trying to do, but the variable substitution should work in bash. You could alternatively try using something like this to loop through all characters in the string:
Code:
while test -n "$strfuture" ; do
        c=${strfuture:0:1}
        echo $c
        strfuture=${strfuture:1}
done

# 5  
Old 02-26-2014
Hi Subbeh,
I am trying to change
Code:
while test $count -le ${CURRENTRUNSCTRL}
      do
          if [ "$count" = 1 ]
          then mj7777_ver='1' start_mj7777_iteration
          fi
          if [ "$count" = 2 ]
          then mj7777_ver='2' start_mj7777_iteration
          fi
          if [ "$count" = 3 ]
          then mj7777_ver='3' start_mj7777_iteration
          fi
          if [ "$count" = 4 ]
          then mj7777_ver='4' start_mj7777_iteration
          fi
          if [ "$count" = 5 ]
          then mj7777_ver='5' start_mj7777_iteration
          fi
          if [ "$count" = 6 ]
          then mj7777_ver='6' start_mj7777_iteration
          fi
          if [ "$count" = 7 ]
          then mj7777_ver='7' start_mj7777_iteration
          fi
          if [ "$count" = 8 ]
          then mj7777_ver='8' start_mj7777_iteration
          fi
          if [ "$count" = 9 ]
          then mj7777_ver='9' start_mj7777_iteration
          fi
          let count=count+1
       done

to this
Code:
strcurrent=123456789
      count=1
      while test $count -le ${CURRENTRUNSCTRL}
      do
          ver=${strcurrent:${count}:1}
          mj7777_ver=${ver} start_mj7777_iteration
          let count=count+1
       done

so it is fewer lines and easier to read or add further threads

Thanks for your suggestion though as I will see if I can use it somehow

Last edited by Franklin52; 02-26-2014 at 10:59 AM.. Reason: Please use CODE tags, not ICODE tags for code blocks. Thanks.
# 6  
Old 02-26-2014
Wouldn't it be easier to use a simple for loop in this case?
Code:
for i in {1..9} ; do
        mj7777_ver="$i" start_mj7777_iteration
done

# 7  
Old 02-26-2014
Thanks Subbeh,
I will need to try as sometimes I need to do fewer than 9 iterations and other times the ver is a character rather than a number but if I can get my head around how to control it, it may work
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

COBOL: Substitution in string

We have a formatted screen system where a driver program passes the locations of a list of files that called programs may be using. It will look something like this: /{number of characters varies}/DATA/MASTERFILEBecause of the size of some files we will be splitting older records into a history... (2 Replies)
Discussion started by: wbport
2 Replies

2. Shell Programming and Scripting

string substitution in perl

Hi, I have a template file and want to replace 3 parameters to the values that I want. these values are in a parameter file. Any idea how to do this in perl? the parameter file looks like: host_name = jupiter PORT = 1562 IPADDRESS = 10.1.34.10 the template file has lots of entry.... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

3. Shell Programming and Scripting

a specific string substitution

hi guys...need some help here... i am making a a script to automatically install netbackup client...so its gonna write a configuration file according to the host name.... the line would be something like this CLIENT_NAME = odel_bkp.test.com the thing is ...the host name in reallity is... (2 Replies)
Discussion started by: razael
2 Replies

4. Shell Programming and Scripting

substitution of string in brackets

Hi friends! I have a tab delimited file with two columns : GB_45_DRB SP:0139466(mrmi sisignm)|SP:3674(fllflg_itoioh)|SP:68954779(RMTKLGF to emmdm-roomto) GB_45_DRD SP:475928(mgmdksi rikgkg)|SP:587959(roykgl tiic-tm)|SP:0139466(mrmi sisignm)|SP:3674(fllflg_itoioh)|SP:68954779(RMTKLGF to... (4 Replies)
Discussion started by: jacks
4 Replies

5. Shell Programming and Scripting

String substitution

Hi, I have a properties file (myprop.properties) which contains some values: @oneValue@==tcp://localhost:1234 @twoValue@==tcp://localhost:4563 @threeValue@==tcp://localhost7895 I have a xml file (myXmlFile.xml)which contains some tokens: <application name="aTest"> <NameValuePair> ... (3 Replies)
Discussion started by: ctrl-alt-del
3 Replies

6. Shell Programming and Scripting

string substitution

Hey ppl, Could u tell me how to replace such a string xyz->x with XYZ(x), where x can be any variable accessible by pointer to structure, xyz in an entire file? (3 Replies)
Discussion started by: laxmi
3 Replies

7. UNIX for Advanced & Expert Users

String Substitution

Hey ppl, Could u tell me how to replace such a string xyz->x with XYZ(x), where x can be any variable accessible by pointer to structure, xyz in an entire file? (1 Reply)
Discussion started by: laxmi
1 Replies

8. Shell Programming and Scripting

Sed - substitution for whole string

Hello I have several files where a string similar to this appears: /home/workload/bin/ProcDly/scrpts/T54.sh > $LOG I need to change it to something like this: $VARIABLE > $LOG However, due to the configuration of the rest of the files, I should only find this string by the... (4 Replies)
Discussion started by: Scarlos
4 Replies

9. Shell Programming and Scripting

String Substitution Question

When I run the script I pass in 2 expressions (ex. replace.ksh new old) I want the script to go line by line for a given file in a given directory and replace the word new with old. Of course in my line where I have the awk statement it is replacing the 2nd word with 1st instead of new with... (3 Replies)
Discussion started by: goodrics
3 Replies

10. UNIX for Dummies Questions & Answers

Sed String Substitution

Hi! I've the following script code with an input parameter: sed 's/oldstring/$1/g' myfile > newfile (I launch it with comman line: $ MyShell newstring) Problem: the substituion doesn't work (oldstring becomes $1, instead of newstring). How could I solve this situation? Thanks, ... (2 Replies)
Discussion started by: pciatto
2 Replies
Login or Register to Ask a Question