LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-20-2011, 06:12 AM   #1
sidw
LQ Newbie
 
Registered: Mar 2008
Posts: 1

Rep: Reputation: 0
Help on using sed to solve my problem


Hi,

I am trying to use sed to do a replacement

I need to replace "$*$" with *

For example in my original file (test.cpp) I have statements like

"#PRINT#"

I need to remove both the " and the # to generate a new file that has just PRINT in the new file

I have tried the following in sed but it does not work

cat test.cpp | sed s/\#(.*)#\/\1/g > test_new.cpp

I get the following error -sed -e expression #1, char 17:invalid reference \1 on s commands RHS

any help on this will be greatly appreciated
 
Old 04-20-2011, 06:47 AM   #2
mayursingru
Member
 
Registered: Nov 2010
Location: Pune
Distribution: CentOS
Posts: 51

Rep: Reputation: 5
Hi sidw,
try the below mentioned thing and let me know.
Code:
 sed -e 's/"#/ /; s/#"/ /' file
 
Old 04-20-2011, 07:00 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,823
Blog Entries: 1

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
Try the following
Code:
sed -r 's/("#|#")//g' infile
 
Old 04-20-2011, 07:33 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Your attempt was close though. post #3 is probably more succinct but your was only missing the following:
Code:
sed -r 's/"#(.*)#"/\1/g' test.cpp > new_test.cpp
 
Old 04-20-2011, 01:18 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by sidw View Post
I need to replace "$*$" with *
I don't have the slightest clue what that means or how it corresponds to your question.

Quote:
Originally Posted by sidw View Post
cat test.cpp | sed ‘s/\”#(.*)#\”/\1/g’ > test_new.cpp

I get the following error -sed -e expression #1, char 17:invalid reference \1 on s commands RHS
First of all, you don't need to escape the double quotes. They don't mean anything to sed, and they won't interfere with your quoting since you used single quotes.

Also, you're using extended regular expressions, but sed uses standard regular expressions. Either use the "-r" option to tell sed to use extended regex, or convert your expression to standard regex:

Code:
cat test.cpp | sed ‘s/”#\(.*\)#”/\1/g’ > test_new.cpp
 
  


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 solve solve broken shell problem prasanth.george Red Hat 1 01-21-2011 09:48 AM
trying to solve a problem... kiasta Linux - General 5 06-28-2006 06:36 PM
SED - can you solve this! 3saul Linux - Software 1 12-24-2005 08:50 PM
help..how i can solve this problem?// TAAN Linux - General 2 02-16-2005 07:53 AM
No one can solve my problem Sundance Linux - Newbie 5 12-08-2003 01:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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