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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-04-2008, 12:18 PM
|
#1
|
|
LQ Newbie
Registered: Jun 2008
Posts: 2
Rep:
|
Help: removing a variable substring from a string variable in sh script
Hi All,
I'm a programmer, but a *nix noobie. Im writing a script that needs to do the following:
=========================
Precondition:
List_String=item1,item2,item3,...itemN
Item_to_be_removed=item2
Magic Happens Here
Postcondition:
List_String=item1,item3,...itemN
Item_to_be_removed=item2
#note: Item_to_be_removed may be item 1 ... itemN, which should be removed the same as this example's item2.
#note2: Item_to_be_removed may be something not even in the list_string, in which case it should fail silently (because that is actually the case we are hoping for)
======================
I'm just not sure what the Magic function is/how to format it
List_String=`echo List_String | sed 's/$Item_to_be_removed//g'` ?
List_String=${$List_String//Item_to_be_removed/} ?
I could just write a compiled program that would do this one function, but I'm certain that there is a better/more elagent way already built in that I can call from the shell script.
Currently looking into awk script.
Thanks in advance,
-Greg
Last edited by gnparsons; 06-04-2008 at 12:56 PM.
Reason: clarification
|
|
|
|
06-04-2008, 04:41 PM
|
#2
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
List_String=`echo List_String | sed 's/$Item_to_be_removed//g'` ?
lose the backquotes
Code:
$
x=item2
$ echo item1,item2,item3,...itemN| sed "s/$x//g"
item1,,item3,...itemN
|
|
|
|
06-04-2008, 05:21 PM
|
#3
|
|
LQ Newbie
Registered: Jun 2008
Posts: 2
Original Poster
Rep:
|
Thanks!
For posterity, the final version of the magic code looked somethign like this:
Code:
List_String=`echo List_String | sed "s/$Item_to_be_removed[,?]//g"`
List_String=`echo List_String | sed "s/[,?]$Item_to_be_removed//g"`
I'm sure that it could be improved (my regex-foo is very weak) but it was sufficient to resolve the issue.
Thanks again,
-Greg
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:29 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|