The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do I perform double substitution in bash? flee Shell Programming and Scripting 3 04-09-2009 04:51 PM
Variable substitution Leo_NN UNIX for Dummies Questions & Answers 7 10-17-2008 11:24 AM
Sed variable substitution when variable constructed of a directory path alrinno Shell Programming and Scripting 2 07-11-2008 02:24 PM
Bash: bad substitution problem...pls help! xfouxs UNIX for Dummies Questions & Answers 1 11-23-2007 05:48 PM
Substitution in a variable spragueg UNIX for Advanced & Expert Users 3 10-18-2001 09:14 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-21-2009
trey85stang trey85stang is offline
Registered User
  
 

Join Date: May 2008
Posts: 66
/bin/bash - variable substitution.

Is it possible with a bash variable to perform multiple substitution strings to one variable?

I have this variable:
echo $clock
TIMEZONE="US/Central"

What I would like to do with bash only it pull out just the "US" part of the variable.. which could be any number of countries.

this is where I am at:

echo ${clock:10}
US/Central"

echo ${clock:10#*/}
bash: testing: 10#*/: syntax error: operand expected (error token is "/")

echo ${clock#*/}
Central"


So, am I just missing something.. or can I only do one type of substition with a bash variable?

thanks,
Trey
  #2 (permalink)  
Old 04-21-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Try this:

Code:
echo $clock | sed 's!.*"\(.*\)/.*!\1!'
Regards
  #3 (permalink)  
Old 04-21-2009
colemar colemar is offline
Registered User
  
 

Join Date: Apr 2009
Location: Trento, Italy
Posts: 116
Code:
colemar@deb:~$ echo ${clock:10:2}
US

colemar@deb:~$ a=${clock#*\"}
colemar@deb:~$ echo $a
US/Central"
colemar@deb:~$ echo ${a%%/*}
US

Last edited by colemar; 04-21-2009 at 04:49 PM.. Reason: %%
  #4 (permalink)  
Old 04-21-2009
trey85stang trey85stang is offline
Registered User
  
 

Join Date: May 2008
Posts: 66
Quote:
Originally Posted by Franklin52 View Post
Try this:

Code:
echo $clock | sed 's!.*"\(.*\)/.*!\1!'
Regards
thanks, but I was really just wondering if I could do this all with bash.

Quote:
Originally Posted by colemar View Post
Code:
colemar@deb:~$ echo ${clock:10:2}
US
I know that, but the problem lies that US is not alway US thus it can be more then 2 characters long...
  #5 (permalink)  
Old 04-21-2009
colemar colemar is offline
Registered User
  
 

Join Date: Apr 2009
Location: Trento, Italy
Posts: 116
Quote:
Originally Posted by trey85stang View Post
I know that, but the problem lies that US is not alway US thus it can be more then 2 characters long...
Then you must do it in two steps, as outlined above.
I believe there is no way to do it at once with bash parameters substitution.
  #6 (permalink)  
Old 04-21-2009
colemar colemar is offline
Registered User
  
 

Join Date: Apr 2009
Location: Trento, Italy
Posts: 116
Quote:
Originally Posted by colemar View Post
I believe there is no way to do it at once with bash parameters substitution.
Seems to be possible, though ugly:
Code:
colemar@deb:~$ echo ${clock//@(*?=\"|\/*?)/}
US
  #7 (permalink)  
Old 04-21-2009
trey85stang trey85stang is offline
Registered User
  
 

Join Date: May 2008
Posts: 66
Quote:
Originally Posted by colemar View Post
Seems to be possible, though ugly:
Code:
colemar@deb:~$ echo ${clock//@(*?=\"|\/*?)/}
US
looks like that will do it, good stuff. I appreciate it. This will save me a lot of work creating a bunch of variables in a script
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:01 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0