Help with sed search&replace between two strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with sed search&replace between two strings
# 1  
Old 07-26-2010
Help with sed search&replace between two strings

Hi, i read couple of threads here on forum, and googled about what bugs me, yet i still can't find solution.
Problem is below.
I need to change this string (with sed if it is possible):
Code:
[rquote=12345&tid=123456&author=nickname]This is message text that is being quoted[/rquote]

to look like this:
Code:
[bquote=nickname]This is message text that is being quoted[/bquote]

I managed to do this by using:
Code:
sed 's/\[rquote=.*author=/[bquote=/g'

Ok, it doesen't replace closing tag [/rquote] to [/bquote], but i can run sed again just to replace that.

That works, until it come do double quote, like this:
Code:
[rquote=12345&tid=123456&author=nickname][rquote=123456&tid=1234567&author=othernick] This is message text that is being quoted [/rquote]

How can i get above example (with double quote) to be replaced with this:
Code:
[bquote=nickname][bquote=othernickname]  This is message text that is being quoted [/bquote]

Some help?

P.S. It is not so impotrant to change those closing tags [/rquote] to [/bquote] in the same sed expression.
# 2  
Old 07-26-2010
Hi,

if your string

Code:
[rquote=12345&tid=123456&author=nickname][rquote=123456&tid=1234567&author=othernick] This is message text that is being quoted [/rquote]

is saved in a file called "file". This:

Code:
sed 's/rquote[^]]*author/bquote/g;s/rquote/bquote/' file

gives me:

Code:
[bquote=nickname][bquote=othernick] This is message text that is being quoted [/bquote]

If you echo your string on the command line, you will have to escape the "&" sings.

HTH Chris
This User Gave Thanks to Christoph Spohr For This Post:
# 3  
Old 07-26-2010
It helps just perfectly! Smilie Tried it on one big post with lots of quotes and it works exelent. Later i will run it over whole db dump and report results.
Big thx Cris!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search and lossless replace strings using sed?

Hello, I would like to replace all occurencies of long data types by others (coresponding int) using 'sed' in the extensive source code of a software package written for 32 bit CPUs. I must use regular expressions to avoid wrong replacements like s/unsigned]+long/ulong/gLeaving out... (2 Replies)
Discussion started by: Mick P. F.
2 Replies

2. Shell Programming and Scripting

Search IP address and replace with strings

Hi All, How can I find (pattern search with grep/awk/sed) all files containing any IP address pattern in one directory hierarchy and in its sub-directories. The files can contains more than 4 octets in addition to the IP address as shown below. IP address can contain any octet combinations like-... (3 Replies)
Discussion started by: sanzee007
3 Replies

3. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

4. Shell Programming and Scripting

Search replace strings between single quotes

Hi All, I need to serach and replace a strings between single quote in a file. My file has : location='/data1/test.log' and the output needed is location='/data2/test_dir/test.log' pls if anybody know this can be done in script sed or awk. I have a script, but it's... (6 Replies)
Discussion started by: mnmonu
6 Replies

5. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

6. Shell Programming and Scripting

Search replace strings between single quotes in a text file

Hi There... I need to serach and replace a strings in a text file. My file has; books.amazon='Let me read' and the output needed is books.amazon=NONFOUND pls if anybody know this can be done in script sed or awk.. i have a list of different strings to be repced by NONFOUND.... (7 Replies)
Discussion started by: Hiano
7 Replies

7. Shell Programming and Scripting

Using sed to replace two different strings?

Hey everyone! Simple question - I am trying to use sed to replace two different strings. As it stands I can implement this as: sed -i 's/TIMEOUT//g' sed -i 's/null//g' And it works. However, is it possible to shrink that down into a single command? Will there be any performance benefits? (3 Replies)
Discussion started by: msarro
3 Replies

8. Shell Programming and Scripting

Search for strings & copy to new file

Hi All, I am just learning shell programming, I need to do the following in my shell script. Search a given log file for two\more strings. If the the two\more strings are found then write it to a outputfile else if only one of the string is found, write the found string in one output... (2 Replies)
Discussion started by: amitrajvarma
2 Replies

9. Shell Programming and Scripting

Help, sed search&replace

Plzzzz, tell me some script about this... What does this mean ? sed '/^ */s///' sed '/^/s// /' and why it's diferent ??? sed '/ */s// /g' and sed 's/ */ /g'. It's all the same ??? Thanks you very much (2 Replies)
Discussion started by: mle
2 Replies

10. Shell Programming and Scripting

Using varible/varible substitution in Perl/sed Search & Replace

Hi, I have a program that searches for a particular string patten. I am however having difficulty passing the varible $i (in a loop) as the string pattern to replace. Using either perl or sed search and replace statements, I get the same kinda result. For example, using the perl: for i in... (3 Replies)
Discussion started by: Breen
3 Replies
Login or Register to Ask a Question