LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-29-2010, 08:27 AM   #1
omarelsewefy
LQ Newbie
 
Registered: Dec 2010
Posts: 2

Rep: Reputation: 0
sed question


Hi All

I have a sed question

I got the following

(12121) and I want to replace the () with xxxx

so the output would be

xxxx12121

I am using the following but its not working

sed 's/\(.*\)/xxxxxx/g' TMP4 > TMP5

emm Please help

Thanks,
Omar
 
Old 12-29-2010, 08:39 AM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hi, welcome to LQ,

try this one
Code:
sed 's/[()]/xxxxxx/g' TMP1 > TMP2
this code substitutes every "(" or ")" with "xxxxxx"

Markus

Last edited by markush; 12-29-2010 at 08:40 AM.
 
Old 12-29-2010, 08:53 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
You need to capture the text inside the parentheses and repeat it in the replacement output. This requires regex backslash referencing.
Code:
sed -r 's/\((.*)\)/xxxxxx\1/g' TMP4
However, the result might still be less-than-desired, as .* is greedy, and depending on the input might grab more than you want. If you'd give us some more details about how the input text is actually formatted, we might be able to narrow it down more.

@markush; I don't think that will satisfy his requirements, as it will put the x's both before and after the string, when he seems to want it to be only before.
 
Old 12-29-2010, 08:56 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

If you only want to substitute the ( and remove the ):

sed -e 's/(/xxxx/g' -e 's/)//g' infile

Hope this helps.

EDIT:
The above also works when there are more substitutions needed on one line.
/EDIT.

Last edited by druuna; 12-29-2010 at 08:58 AM.
 
Old 12-29-2010, 09:11 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi (again),

This seems to work if there are numbers between the ( and ):

sed -r 's/\(([0-9][0-9]*)\)/xxxxx\1/g' infile

But as already stated by David the H., without an exact example of the input things might or might not work as expected.
 
Old 12-29-2010, 09:16 AM   #6
omarelsewefy
LQ Newbie
 
Registered: Dec 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by druuna View Post
Hi (again),

This seems to work if there are numbers between the ( and ):

sed -r 's/\(([0-9][0-9]*)\)/xxxxx\1/g' infile

But as already stated by David the H., without an exact example of the input things might or might not work as expected.

Thanks alot this is working exactly as I want it to

Omar
 
Old 12-30-2010, 10:58 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Actually, probably the safest way to do it, without more info, would be this:
Code:
sed -r 's/\(([^)]*)\)/xxxxxx\1/g' infile.
This ensures that the match stops at the first closing parenthesis, capturing everything between them, regardless of what it is.
 
  


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
Sed question wondergirl Programming 21 02-22-2008 03:42 AM
Question about sed IceOner Programming 7 12-13-2007 10:55 AM
sed question? slack66 Linux - Newbie 7 07-26-2006 07:47 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
A question on 'sed' Barbarian Programming 4 04-11-2002 11:19 PM

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

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