LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2010, 08:52 AM   #1
fitteschleiker
LQ Newbie
 
Registered: Feb 2004
Location: perth, australia
Distribution: slackware
Posts: 22

Rep: Reputation: 15
Question bash substring manipulation problem


Hi my script has a variable which comes in the form +00.00 +0.00 -00.00 or -0.00 (the numbers can be any in that form)

for any that have a + symbol I need to remove the +, but if it has a - symbol it needs to stay. i need to make a new variable with the string from the old variable btut without any plus sign.

I have tried a lot of different ways with no success, each thing I tried either left the + or removed the entire string.

I think this should work but doesn't

foo=+12.40
bar=${foo#+}

any ideas anyone?
 
Old 04-07-2010, 08:59 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
c@CW8:~$ foo=+12.40
c@CW8:~$ echo ${foo//+/}
12.40
EDIT:
Code:
c@CW8:~$ foo='+00.00 +0.00 -00.00 or -0.00'
c@CW8:~$ echo ${foo//+/}
00.00 0.00 -00.00 or -0.00

Last edited by catkin; 04-07-2010 at 09:01 AM.
 
Old 04-07-2010, 09:12 AM   #3
fitteschleiker
LQ Newbie
 
Registered: Feb 2004
Location: perth, australia
Distribution: slackware
Posts: 22

Original Poster
Rep: Reputation: 15
OMG thankyou

I have been searching everywhere!

and thats not in my book...

in fact looking at the man page, I still don't quite understand what you did..

I'm guessing ${parameter/pattern/string}
Pattern substitution.

well thanks again, been pulling my hair out, now i can finish this bugger
 
Old 04-07-2010, 09:38 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
You can check it out here:

http://tldp.org/LDP/abs/html/string-manipulation.html
 
Old 04-07-2010, 12:13 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by fitteschleiker View Post
I'm guessing ${parameter/pattern/string}
Pattern substitution.
Glad it is solved for you The // means global (all instances) replace. Reference info here (scroll down to last expansion). Personally I find the GNU Bash Reference easier to use (table of contents and all on one page) than the Advanced Bash Scripting Guide (as per grail's post) but the ABS has examples which can be helpful. Actually the GNU Bash Reference is very close to the man page:
Code:
       ${parameter/pattern/string}
       ${parameter//pattern/string}
              The pattern is expanded to produce a pattern just as in pathname expansion.  Parameter is expanded and the  longest  match  of
              pattern  against  its  value  is  replaced with string.  In the first form, only the first match is replaced.  The second form
              causes all matches of pattern to be replaced with string.  If pattern begins with #, it must match at  the  beginning  of  the
              expanded  value  of  parameter.   If  pattern  begins with %, it must match at the end of the expanded value of parameter.  If
              string is null, matches of pattern are deleted and the / following pattern may be omitted.  If parameter is @ or *,  the  sub‐
              stitution operation is applied to each positional parameter in turn, and the expansion is the resultant list.  If parameter is
              an array variable subscripted with @ or *, the substitution operation is applied to each member of the array in turn, and  the
              expansion is the resultant list.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: test for substring inside string ali_bush Linux - General 3 04-26-2018 07:31 PM
bash replace all matches of regex substring in string nickleus Linux - General 3 04-30-2011 11:08 AM
Bash - Substring Extraction and Substitution on same variable jax8 Programming 5 04-26-2009 06:20 AM
using awk substring function on a file in a bash script matt007 Programming 3 06-17-2008 08:17 PM
get a substring with a bash command xeon123 Linux - General 4 01-26-2007 03:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration