LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed question (https://www.linuxquestions.org/questions/linux-newbie-8/sed-question-853009/)

omarelsewefy 12-29-2010 07:27 AM

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

markush 12-29-2010 07:39 AM

Hi, welcome to LQ,

try this one
Code:

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

Markus

David the H. 12-29-2010 07:53 AM

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.

druuna 12-29-2010 07:56 AM

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.

druuna 12-29-2010 08:11 AM

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.

omarelsewefy 12-29-2010 08:16 AM

Quote:

Originally Posted by druuna (Post 4206551)
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

David the H. 12-30-2010 09:58 AM

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.


All times are GMT -5. The time now is 04:30 AM.