LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Replace a string @CURRANGE("***","***") to @CURRANGE("xxx","xxx") in a file (http://www.linuxquestions.org/questions/linux-newbie-8/replace-a-string-%40currange-%2A%2A%2A-%2A%2A%2A-to-%40currange-xxx-xxx-in-a-file-936474/)

mavadikarmayur 03-26-2012 06:57 AM

Replace a string @CURRANGE("***","***") to @CURRANGE("xxx","xxx") in a file
 
Hi all,

I have a file in which I need to find and replace a string @CURRANGE("***","???") to @CURRANGE("---","^^^") where ***, ???, --- and ^^^ can be any string. I feel I need to use sed to do it but if someone could be more specific that would be much more helpful.

Thank You.
Mayur.

colucix 03-26-2012 07:25 AM

Hi Mayur and welcome to LinuxQuestions!

Here is an example:
Code:

sed 's/@CURRANGE("[^"]*","[^"]*")/@CURRANGE("new_string_one","new_string_two")/g' file
where the expression [^"]* means zero or any number of characters different than ". This ensures that two or more expressions in the same line (if any) are changed accordingly. Example:
Code:

$ cat file
text @CURRANGE("any string","yet another string") text @CURRANGE("whatever","235472934629746") text
$
$ sed 's/@CURRANGE("[^"]*","[^"]*")/@CURRANGE("new_string_one","new_string_two")/g' file
text @CURRANGE("new_string_one","new_string_two") text @CURRANGE("new_string_one","new_string_two") text

Use the -i option of sed if you want to edit the file in place. Use -i.bck to keep a backup copy of the original file, that will be named by adding the (custom) suffix .bck. Hope this helps.

mavadikarmayur 03-26-2012 08:10 AM

Hi colucix:cool:,

Brilliant :cool: !!

Worked very Smooth !!

Thanks so much !!!!!

colucix 03-26-2012 08:32 AM

You're welcome! :)


All times are GMT -5. The time now is 08:13 AM.