Double Substitution variables in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Double Substitution variables in ksh
# 15  
Old 05-20-2007
Hi

I am even more confused regarding this regular expression

'.*/\(.*\)\..*'

can somebody please explain that.

I tried googling on this but that did not help much
# 16  
Old 05-20-2007
Well maybe this will help... the pieces in red are special:
'.*/\(.*\)\..*'
They define the beginning and end of a saved field. This is what will be be returned. The stuff to letf of the saved field is .*/ which matches everything up to a slash. Inside the saved field we have .* which seems to say match any characters. But we can't match literally everything, we have to save (just) enough for the rest of the expression to work. So after the saved field we have \..* which says match a dot followed by any characters.
# 17  
Old 05-20-2007
Quote:
Originally Posted by aigles
[...]
works fine under zsh ( but i don't think that there is many people using this shell)
[...]
Yes,
unfortunately ...
# 18  
Old 05-23-2007
Quote:
Originally Posted by Perderabo
Well maybe this will help... the pieces in red are special:
'.*/\(.*\)\..*'
They define the beginning and end of a saved field. This is what will be be returned. The stuff to letf of the saved field is .*/ which matches everything up to a slash. Inside the saved field we have .* which seems to say match any characters. But we can't match literally everything, we have to save (just) enough for the rest of the expression to work. So after the saved field we have \..* which says match a dot followed by any characters.
Hi Perderabo

Thanks for the wunderful explanation

so what i understand is
.*/ Matches upto /
\(.*\) Matches and saves myfile.cd.070505123457
\..* Match a dot followed by any characters

So shouldnt this match just cd.070505123457 and the whole expression return myfile how come it is returning myfile.cd

Thanks for being so patient with me.

regards
Hrishy

Last edited by xiamin; 05-23-2007 at 03:46 AM..
# 19  
Old 05-23-2007
Regular expression quantifiers are greedy, so "*" means match as much as possible.

.*/ Matches any characters upto the last /
\(.*\)\. Matches and returns any characters upto the last .
.* not actually required. (I originally thought that this was required because expr regular expressions are anchored, but it turns out that they are only achored to the start of the expression.)
# 20  
Old 05-23-2007
Hi Ygor

Thanks ..a million i got it.

Gotta remember this "regular expressions are greedy "

regards
Hrishy
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problems with substitution between two variables

To all geeks, What I want to achieve: 1. Accept two filenames from user and store the filenames in two variables (FILE1 and FILE2) 2. Check if files exisits. If doesn't, then exit 3. If files exist, look for a particular string in both files 4. If the string exists, then change the... (8 Replies)
Discussion started by: Deepak Tulsani
8 Replies

2. UNIX for Dummies Questions & Answers

sed substitution and shell variables

Hello!! Am trying to substitute the value of a shell variable with the value of another shell variable. The values are obtained into the shell variables through some other processing. for ex. i've used the follow sed command.. sed "s/$var1/$var2/g" i've also tried the other... (5 Replies)
Discussion started by: a_ba
5 Replies

3. Shell Programming and Scripting

bad substitution error in ksh

hi, i created a shell script having the following content: #! /usr/bin/ksh FROM="myemail@domain.com" MAILTO="someemail@domain" SUBJECT="TEST" BODY="/export/home/adshocker/body.txt" ATTACH="/export/home/adshocker/attach.prog" echo $ATTACH ATTACH_NAME="${ATTACH##*/}" echo $ATTACH_NAME... (5 Replies)
Discussion started by: adshocker
5 Replies

4. Solaris

Shell variables substitution in a file

Hi, I have to read a file and translate the contents including substituting the variables if any and write to another file without using sed or awk. For ex:- inputfile.txt ----------- servername=$SERVER application=$APPL outputfile.txt ------------ servername=actual server name... (2 Replies)
Discussion started by: axes
2 Replies

5. Shell Programming and Scripting

variable substitution in ksh

Hi I have a variable BIT1 which holds some value. Is there a way to retrieve the value of this variable indirectly via another variable, lets say SUBSET_BIT_NUM=1, so the call will look something like this: sundev1 $ echo ${BIT${SUBSET_BIT_NUM}} ksh: ${BIT${SUBSET_BIT_NUM}}: bad substitution ... (3 Replies)
Discussion started by: aoussenko
3 Replies

6. UNIX for Advanced & Expert Users

shell variables and sed substitution

My specific goal: automatically edit a Makefile variable's (EXTRAVERSION) value in a kernel Makefile. So, I have a shell script that takes one parameter, a version string: buildkernel.sh 2.6.18.21.7-custom In the script, I assign the parameter to K_VER: K_VER="2.6.18.21.7-custom"... (2 Replies)
Discussion started by: duderonomy
2 Replies

7. Solaris

Substitution not working in ksh

Hi, Following code is working in bash but not in ksh. Can someone please send me an alternative? #!/bin/ksh fname="EOA.dmp" echo $fname logname=${fname/.dmp/.log} echo $logname I am getting below error in ksh "testcmd: logname=${fname/.dmp/.log}: 0403-011 The specified substitution... (3 Replies)
Discussion started by: arsheshadri
3 Replies

8. AIX

Substitution not working in ksh

Following code is working in bash but not in ksh. Can someone please send me an alternative? #!/bin/ksh fname="EOA.dmp" echo $fname logname=${fname/.dmp/.log} echo $logname I am getting below error in ksh "testcmd: logname=${fname/.dmp/.log}: 0403-011 The specified substitution is not... (2 Replies)
Discussion started by: arsheshadri
2 Replies

9. Shell Programming and Scripting

KSH variable substitution

Hi folks Please let me know if anyone knows how to handle this. My KSH script -> testscript.ksh cmd=$1 ENV="devl" echo $cmd This is how I call the script ./testscript.ksh 'ps -ef | grep br$ENV' How do I get this to print the below text i.e $ENV should be substituted with the value... (5 Replies)
Discussion started by: tipsy
5 Replies

10. Shell Programming and Scripting

ksh substitution

Hello, I thought it was possible to use several time a #! entry on a script but it doesn't seems to work. My need is to have a part of a ksh script without substitution so it would look like #!/bin/ksh -- first part --- #!/bin/ksh -f -- part without substitution -- #!/bin/ksh --... (2 Replies)
Discussion started by: solea
2 Replies
Login or Register to Ask a Question