LinuxQuestions.org
Visit Jeremy's Blog.
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 03-11-2011, 04:29 AM   #1
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
substitute ( in sed


I want to substitute every occurance of "( S" to "(S" in my file (the quotes aren't included in my file. Its just there to specify what my pattern is)

This is my attempt to do it and the error message I get.
Code:
denish@ubuntu:~/Desktop/tgrep files$ sed 's/^\( \(S$/\(S/g' test > test2
sed: -e expression #1, char 16: Unmatched ( or \(
The "\( \)" has a different purpose in sed. So how exactly do I specify a ( in sed as a pattern and not as the special function?
 
Old 03-11-2011, 04:33 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
To take it literally, enclose the parenthesis in a character list:
Code:
sed 's/[(] S/(S/g' test
 
Old 03-11-2011, 04:33 AM   #3
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
You need to use ^( in the search field of said, not \(

Edit - colucix beat me to it. You can also do it his way.
 
Old 03-11-2011, 04:34 AM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
What about:
Code:
sed 's/( S/(S/g' file
 
1 members found this post helpful.
Old 03-11-2011, 04:37 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Or eventually use the -r option and escape the parenthesis. In extended regular expression mode, sed will recognize plain parentheses as the symbols to enclose a pattern to keep, whereas the escaped one will be a literal parenthesis:
Code:
sed -r 's/\( S/(S/g' test
Edit: sycamorex's simple and direct solution works for me. Indeed it's the opposite of what I have just explained: in normal mode the plain parenthesis will be literal.

Last edited by colucix; 03-11-2011 at 04:40 AM.
 
Old 03-11-2011, 04:38 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by colucix View Post
To take it literally, enclose the parenthesis in a character list:
Code:
sed 's/[(] S/(S/g' test
Is it absolutely necessary? Plain:
Code:
sed 's/^( S/(S/' infile
seems to work as well.

Last edited by sycamorex; 03-11-2011 at 05:21 AM.
 
Old 03-11-2011, 04:39 AM   #7
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
^( always worked for me...
 
Old 03-11-2011, 10:46 AM   #8
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Thanks for the help. I was just being silly. ( isn't a special character to there's no need for \ infront of it. I tired doing it without the \ before posting but I messed up the syntax so I thought none of those method work. Again thanks for all your help.
 
  


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
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
sed : substitute without displacing columns billywayne Programming 13 05-27-2010 08:33 PM
[SOLVED] sed substitute everything until character sqn Programming 5 03-30-2010 10:27 AM
[SOLVED] SED - substitute a word only in a certain line carolflb Linux - Newbie 3 02-02-2010 09:30 AM
Sed substitute for my username? camaroblue87 Linux - Newbie 1 04-29-2006 11:07 AM

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

All times are GMT -5. The time now is 06: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
Open Source Consulting | Domain Registration