Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-04-2008, 01:18 PM   #1
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:



Magic Happens Here



#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,

Last edited by gnparsons; 06-04-2008 at 01:56 PM. Reason: clarification
Old 06-04-2008, 05:41 PM   #2
Senior Member
Registered: Mar 2004
Location: england
Distribution: Void, Debian, Puppy, Raspbian
Posts: 3,482

Rep: Reputation: 223Reputation: 223Reputation: 223
List_String=`echo List_String | sed 's/$Item_to_be_removed//g'` ?

lose the backquotes

$ echo item1,item2,item3,...itemN| sed "s/$x//g"
Old 06-04-2008, 06:21 PM   #3
LQ Newbie
Registered: Jun 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Thumbs up


For posterity, the final version of the magic code looked somethign like this:

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,


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:09 PM.

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