LinuxQuestions.org
Review your favorite Linux distribution.
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 06-04-2008, 12: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 12:56 PM. Reason: clarification
 
Old 06-04-2008, 04:41 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
1 members found this post helpful.
Old 06-04-2008, 05: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



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

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

All times are GMT -5. The time now is 05:56 PM.

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