LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-04-2008, 01:18 PM   #1
gnparsons
LQ Newbie
 
Registered: Jun 2008
Posts: 2

Rep: Reputation: 0
Question 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 01:56 PM. Reason: clarification
 
Old 06-04-2008, 05:41 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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
 
Old 06-04-2008, 06:21 PM   #3
gnparsons
LQ Newbie
 
Registered: Jun 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Thumbs up

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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to assign a string to a structure variable in C? daYz Programming 7 06-13-2007 03:38 PM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 12:23 PM
Need shell script to concatenate a string and a variable into a directory name AwesomeMachine Linux - Newbie 2 05-07-2006 04:42 AM
variable to string x2000koh Programming 4 07-30-2003 03:23 AM
Getting a variable name based on a string. jtshaw Programming 7 10-08-2002 03:06 PM


All times are GMT -5. The time now is 09:48 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration