Sed command to replace with pattern except for text and closing parentheses


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed command to replace with pattern except for text and closing parentheses
# 1  
Old 01-09-2012
Sed command to replace with pattern except for text and closing parentheses

Can someone help me with a sed command:
There will be multiple occurences in a file that look like this:
MyFunction(12c34r5)
and I need to replace that with just the 12c34r5 for every occurrence. The text between the parentheses will be different on each occurrence, so I can't search for that. But the MyFunction text will always be the same, and it will always have an opening and closing parentheses around the text. Having trouble figuring out the sed command that uses the pattern in the replacement "Except for" part of the pattern?
# 2  
Old 01-09-2012
Code:
$ cat inputfile
MyFunction(12c34r5) MyFunction(12c34r5) MyFunction(12c34r5)
MyFunction(abc) MyFunction(12c34r5)
MyFunction(123)
$
$ sed 's/MyFunction(\([^)]*\))/\1/g' inputfile
12c34r5 12c34r5 12c34r5
abc 12c34r5
123

Use -i switch for in-file replacement.
# 3  
Old 01-09-2012
sed

Hi,

Try this,

Code:
sed 's/\(.*\)MyFunction(\([0-9a-zA-Z]*\))\(.*\)/\1\2\3/g' FileName

Cheers,
RangaSmilie
# 4  
Old 01-09-2012
Quote:
Originally Posted by rangarasan
Code:
sed 's/\(.*\)MyFunction(\([0-9a-zA-Z]*\))\(.*\)/\1\2\3/g' FileName

@rangarasan: This would greedily match the whole line (courtesy, .* at the beginning of regex) and then give up text one by one until it matched MyFunction(blah). Thus, this would replace only the last occurrence of MyFunction(blah) in a line. Please check.
# 5  
Old 01-09-2012
using tr
Code:
$ tr -d 'MyFunction()' < infile
1234r5 1234r5 1234r5
ab 1234r5
123

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with awk or sed Command to Replace Text in Files

Hello Everyone, I have many files like so: file1.txt file2.txt file3.txt Within each file I have many lines of random text separated by commas like so: abcAAA,123,defAA,456777,ghiA,789 jklB,101,mnoBBB,11211,pqrB,13111 stuCC,415,vwxCCCC,161,yzaC,718 I am trying to use SED or AWK to... (4 Replies)
Discussion started by: D3U5X
4 Replies

2. Shell Programming and Scripting

Pattern replace from a text file using sed

I have a sample text format as given below <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456" From="1626711840902323"... (3 Replies)
Discussion started by: my_Perl
3 Replies

3. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

5. Shell Programming and Scripting

sed help, Find a pattern, replace it with same text minus leading 0

HI Folks, I'm looking for a solution for this issue. I want to find the Pattern 0/ and replace it with /. I'm just removing the leading zero. I can find the Pattern but it always puts literal value as a replacement. What am I missing?? sed -e s/0\//\//g File1 > File2 edit by... (3 Replies)
Discussion started by: SirHenry1
3 Replies

6. Shell Programming and Scripting

pattern replace inside text file using sed

Hi, I have a situation where I want to replace some occurrences of ".jsp" into ".html" inside a text file. For Example: If a pattern found like <a href="http://www.mysite.com/mypage.jsp"> it should be retained. But if a pattern found like <a href="../mypage.jsp"> it should be changed to... (4 Replies)
Discussion started by: meharo
4 Replies

7. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

8. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies

9. Shell Programming and Scripting

Replace text in parentheses

Hi I would like to replace a comma in parentheses to a semicolon for example. Other commas outside () stay unchanged. How can I do this? aaaa,bbb,ccc,ddd(eee,fff,ggg),hhh,iii to aaaa,bbb,ccc,ddd(eee;fff;ggg),hhh,iii Thanks (5 Replies)
Discussion started by: lalelle
5 Replies

10. Shell Programming and Scripting

Parentheses in perl find/replace

I'm trying to use the following command to do a batch find and replace in all commonly named files through a file hierarchy find . -name 'file' |xargs perl -pi -e 's/find/replace/g' which works fine except for a substitution involving parenthesis. As a specific example I'm trying to sub... (3 Replies)
Discussion started by: Jeffish
3 Replies
Login or Register to Ask a Question