Parameter substitution with##


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parameter substitution with##
# 1  
Old 08-04-2012
Parameter substitution with##

Hi experts

I want to use the parameter substitution in the bash with ## to get

a=mfs1000 (not the "mfs" maybe other string and the length is not the same"

I want to get

1000

any help? I don't know use which pattern

I use
Code:
echo ${a##*[a-b]}

It doesn't work
Lei
# 2  
Old 08-04-2012
If what you want to do is get rid of any non-numeric characters at the start of the variable:
Code:
echo "${a##*[^0-9]}"

# 3  
Old 08-04-2012
Quote:
Originally Posted by Don Cragun
If what you want to do is get rid of any non-numeric characters at the start of the variable:
Code:
echo "${a##*[^0-9]}"

yes it works thank

another quesion is

a=mfs12345689

I want to get below which get rid of the no-numeric characters and the first four digital

5689

Code:
echo "${a##*[^0-9][0-9]{4}}"

it doesn't work any help
# 4  
Old 08-04-2012
When using ${var##...}, ${var#...}, ${var%%...}, and ${var%...} the ... is a pattern. Patterns and regular expressions are similar, but not the same. This should give you what you want:
Code:
echo "${a##*[^0-9][0-9][0-9][0-9][0-9]}"

# 5  
Old 08-05-2012
Can I say "${var##...}, ${var#...}, ${var%%...}, and ${var%...} only support the "*" "[0-9]" "[a-z]" "[A-Z", it seems others are not support " eg "." {3,}" "^" "$"

Thanks

Lei
# 6  
Old 08-05-2012
Quote:
Originally Posted by yanglei_fage
Can I say "${var##...}, ${var#...}, ${var%%...}, and ${var%...} only support the "*" "[0-9]" "[a-z]" "[A-Z", it seems others are not support " eg "." {3,}" "^" "$"

Thanks

Lei
Given the hints I've supplied so far, I'd expect that you'd be able to decode the bash man page to answer this. The example I gave above should work with any POSIX-conforming shell. I don't know of any shell where:
Code:
echo "${a##*[^0-9][0-9]{4}}"

will work. I use ksh for most of my shell programming. With ksh, but not bash:
Code:
echo "${a##*[^0-9]{4}([0-9])}"

will work.

If you can't figure out the bash man page and you don't want to restrict yourself to the set of portable shell constructs specified by the POSIX standard, you'll need to find someone else to help you.

Sorry,
Don
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parameter substitution is not working with sed

I am trying add a prefix variable(string) to command output. sed parameter substitution is not working. - I have found some issues on my end of testing,, please delete this thread for now. (1 Reply)
Discussion started by: kchinnam
1 Replies

2. Shell Programming and Scripting

Does awk have parameter substitution?

Can I specify a default value to a variable in AWK like BASH in one statement using parameter substitution? BASH example: argument=${$1-"default if empty"} (BASH) I know I can do: argument=$1; sub ( "^$", "default if empty", argument) (AWK) Mike (13 Replies)
Discussion started by: Michael Stora
13 Replies

3. Shell Programming and Scripting

Call Script with Parameter (that has another parameter)

Hi. How do I achieve this sh /EDWH-DMT02/script/MISC/exec_sql.sh "@/EDWH-DMT02/script/others/CSM_CKC/Complete_List.sql ${file_name}" Complete_List.txt The /EDWH-DMT02/script/MISC/exec_sql.sh has two parameters and it's working fine with this sh /EDWH-DMT02/script/MISC/exec_sql.sh... (7 Replies)
Discussion started by: aimy
7 Replies

4. Shell Programming and Scripting

Resolving a parameter which is passed as parameter

Hi, I have the following files. ->cat scr.sh export TMP_DIR=/home/user/folder1 export TMP_DIR_2=/home/user/folder2 while read line do cat "$line" done<file_list.dat ------------------------ -> cat file_list.dat $TMP_DIR/file1.txt $TMP_DIR_2/file2.txt --------------------------- -> cat... (6 Replies)
Discussion started by: barath
6 Replies

5. Shell Programming and Scripting

How to get the parameter value from the parameter file in perl?

hi all, i have a parameter file of following format, i want a method which can get the value of specific parameter. parameter file format: <Parameter Name="FileLocationWindows"> <Description> The directory location of the logger file. ... (1 Reply)
Discussion started by: laxmikant.hcl
1 Replies

6. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

7. Shell Programming and Scripting

Command that takes one parameter and then searches for the passed in parameter

Hi I am looking for a unix command or a small shell script which can takes one parameter and then searches for the passed in the parameter in any or all files under say /home/dev/ Can anyone please help me on this? (3 Replies)
Discussion started by: pankaj80
3 Replies

8. UNIX for Dummies Questions & Answers

Parameter substitution with alias

Hello, in my .bashrc I tried to setup some aliases. alias scp_cmd="scp -P 8888 $1 me@somehost:." is supposed to copy a local file to somehost via scp. However it seems that the command line substitution does not work here. However this works: alias lst="ls -l $1" The above scp command can... (1 Reply)
Discussion started by: strobotta
1 Replies

9. Shell Programming and Scripting

Difference between "Command substitution" and "Process substitution"

Hi, What is the actual difference between these two? Why the following code works for process substitution and fails for command substitution? while IFS= read -r line; do echo $line; done < <(cat file)executes successfully and display the contents of the file But, while IFS='\n' read -r... (3 Replies)
Discussion started by: royalibrahim
3 Replies

10. Shell Programming and Scripting

how do I make dynamic parameter names? Or get the value of a parameter evaluated twi

Say I write something like the following: var1=1 var2=2 for int in 1 2 do echo "\$var$int" done I want the output to be: 1 2 Instead I get something like: $var1 $var2 (2 Replies)
Discussion started by: Awanka
2 Replies
Login or Register to Ask a Question