LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 06-01-2010, 11:53 AM   #1
mahmoodn
Member
 
Registered: May 2010
Posts: 403

Rep: Reputation: 16
string manipulation in a script file


I have a string like $(SOMETHING) and want to remove '(' and ')' in a script file. How can I do that? I read that sed command is a filtering utility but don't know how to use it.
 
Old 06-01-2010, 12:05 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Sed is a command line editor. Basically it parses a text and do actions based on regular expressions. The 's' command is one of the more used in one-liners: in this case you can substitute every occurrence of '(' and ')' with nothing, e.g.
Code:
string='$(SOMETHING)'
string=$(echo $string | sed 's/[()]//g')
Without sed you can use the shell's substring replacement, but you need two passes:
Code:
string='$(SOMETHING)'
string=${string/(/}
string=${string/)/}
To learn about sed, here is a must-read: http://www.grymoire.com/Unix/Sed.html
 
Old 06-01-2010, 12:50 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
If the string is in a bash variable and SOMETHING does not include any "(" or ")" you can do it with two bash parameter expansions like this
Code:
string='$(SOMETHING)'
string=${string/(/}
string=${string/)/}
That will run faster than using sed (which may be insignificant) but the sed solution is arguably more legible.

EDIT: Ooops! Colucix already wrote that

EDIT 2: Now, new, improved! In a single command
Code:
string=${string//[()]/}

Last edited by catkin; 06-01-2010 at 12:54 PM.
 
Old 06-01-2010, 01:03 PM   #4
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,019

Rep: Reputation: 63
Quote:
Originally Posted by catkin View Post
If the string is in a bash variable and SOMETHING does not include any "(" or ")" you can do it with two bash parameter expansions like this
Code:
string='$(SOMETHING)'
string=${string/(/}
string=${string/)/}
That will run faster than using sed (which may be insignificant) but the sed solution is arguably more legible.

EDIT: Ooops! Colucix already wrote that

EDIT 2: Now, new, improved! In a single command
Code:
string=${string//[()]/}
Can't understand how it works.
Code:
string=${string//[()]/}
 
Old 06-01-2010, 01:54 PM   #5
mahmoodn
Member
 
Registered: May 2010
Posts: 403

Original Poster
Rep: Reputation: 16
Thumbs up

Code:
string=$(echo $string | sed 's/[()]//g')
and
Code:
string=${string//[()]/}
are the same, however the syntax of the second is complex for me...

Thank you all
 
Old 06-01-2010, 02:17 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
What's so difficult to understand? It's all explained in the link catkin provided.

${variable}

The full form of $variable, necessary in some complex situations.

${variable//foo/bar}

Replace all instances of "foo" with "bar" in the variable when expanding it.

[]

Allows groupings of multiple characters. [ABC] would mean A or B or C.

So ${string//[()]/} simply means "replace all instances of ( or ) with nothing when outputting $string, exactly like in the sed statement.
 
1 members found this post helpful.
Old 06-01-2010, 07:44 PM   #7
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,019

Rep: Reputation: 63
This should have been like this:
Code:
string=${string/[()]//}

Last edited by cola; 06-01-2010 at 07:48 PM.
 
Old 06-01-2010, 11:58 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Quote:
Originally Posted by cola View Post
This should have been like this:
Code:
string=${string/[()]//}
Um, why? This version replaces only the first parenthesis found with a slash. It's a version of ${variable/foo/bar}, which only replaces one instance, like sed without the global 'g' command.

Code:
$ variable="this is a (foo) (bar) string"

$ echo "${variable/[()]//}"
this is a /foo) (bar) string

$ echo "${variable//[()]/}"
this is a foo bar string
Edit: Maybe you're getting it confused with the sed syntax. Unlike sed, there's no final slash. Only the ending '}' bracket. And instead of 's' at the front and 'g' at the end, there are one or two slashes at the beginning.

Code:
echo "${variable/foo/bar}"  =  echo "$variable | sed 's/foo/bar/'"
echo "${variable//foo/bar}"  =  echo "$variable | sed 's/foo/bar/g'"

Last edited by David the H.; 06-02-2010 at 12:04 AM.
 
Old 06-02-2010, 02:47 AM   #9
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by catkin View Post
Code:
string=${string//[()]/}
Yesss! I always forget about that!
 
Old 06-02-2010, 03:12 AM   #10
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,019

Rep: Reputation: 63
Quote:
Originally Posted by David the H. View Post
Um, why? This version replaces only the first parenthesis found with a slash. It's a version of ${variable/foo/bar}, which only replaces one instance, like sed without the global 'g' command.

Code:
$ variable="this is a (foo) (bar) string"

$ echo "${variable/[()]//}"
this is a /foo) (bar) string

$ echo "${variable//[()]/}"
this is a foo bar string
Edit: Maybe you're getting it confused with the sed syntax. Unlike sed, there's no final slash. Only the ending '}' bracket. And instead of 's' at the front and 'g' at the end, there are one or two slashes at the beginning.

Code:
echo "${variable/foo/bar}"  =  echo "$variable | sed 's/foo/bar/'"
echo "${variable//foo/bar}"  =  echo "$variable | sed 's/foo/bar/g'"
Isn't the form to replace string:
Code:
st="bgerretsberger"
st=${st/et/zz}
echo ${st}
It's common for sed witout g and for vim too.
 
Old 06-02-2010, 08:34 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Quote:
Originally Posted by cola View Post
Isn't the form to replace string:
Code:
st="bgerretsberger"
st=${st/et/zz}
echo ${st}
It's common for sed witout g and for vim too.
Please read the link and my previous post again and pay careful attention to the syntax.

One slash at the front is for a single replacement.
Two slashes at the front is for a global replacement.

It's the same as the difference between sed "s///" and sed "s///g"

Since the opening poster wanted to have all parentheses removed, he'd have to use the two-slash version.

In fact, if all you want to do is remove a substring, you don't even need the final slash. The replacement will automatically be "nothing".

Code:
$ variable="this is a foo foo foo string"

$ echo "${variable/foo }"  #note the space at the end
this is a foo foo string

$ echo "${variable//foo }"
this is a string

$ echo "${variable/foo /bar }"
this is a bar foo foo string

$ echo "${variable//foo /bar }"
this is a bar bar bar string

### Now with sed:

$ echo "$variable" | sed 's/foo //'
this is a foo foo string

$ echo "$variable" | sed 's/foo //g'
this is a string

$ echo "$variable" | sed 's/foo /bar /'
this is a bar foo foo string

$ echo "$variable" | sed 's/foo /bar /g'
this is a bar bar bar string
Just try it for yourself if you aren't convinced.
 
Old 06-02-2010, 11:17 AM   #12
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,019

Rep: Reputation: 63
Quote:
Originally Posted by David the H. View Post
Um, why? This version replaces only the first parenthesis found with a slash. It's a version of ${variable/foo/bar}, which only replaces one instance, like sed without the global 'g' command.

Code:
$ variable="this is a (foo) (bar) string"

$ echo "${variable/[()]//}"
this is a /foo) (bar) string

$ echo "${variable//[()]/}"
this is a foo bar string
Edit: Maybe you're getting it confused with the sed syntax. Unlike sed, there's no final slash. Only the ending '}' bracket. And instead of 's' at the front and 'g' at the end, there are one or two slashes at the beginning.

Code:
echo "${variable/foo/bar}"  =  echo "$variable | sed 's/foo/bar/'"
echo "${variable//foo/bar}"  =  echo "$variable | sed 's/foo/bar/g'"
Why does it change only the first '(' character?
And why is it replaced with '/'?
 
Old 06-02-2010, 02:39 PM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Figure it out.

${variable/[()]//}

"variable slash X slash Y".

So what is X, and what is Y, in the example above?

And what did I just say about the difference between having one slash and two in the first position?
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
script for file manipulation yongitz Programming 3 08-17-2006 12:22 PM
String manipulation with BASH script King V Programming 9 04-21-2006 04:15 PM
shell script: File Manipulation simon_qwl Programming 2 10-29-2005 04:40 PM
String manipulation with a script. philipina Programming 4 03-16-2004 03:42 PM
String manipulation with a script? philipina General 1 03-15-2004 01:07 PM


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