LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-21-2004, 05:37 AM   #1
gd13
LQ Newbie
 
Registered: Dec 2004
Posts: 2

Rep: Reputation: 0
SED;find and replace;help required


I wrote a script to replace "old_var" by "new_var" in all files in the current directory.
Now, what i wanted was the script reads a lsit of "old_var" and "new_var" from an input file
for eg: the input file is old_new.txt contains
a1 a2
b1 b2
c1 c2......

Here is my script
-------------------------------------------------------
#!/bin/bash

while read old_var new_var line
do

for file in $(grep -l $old_var *)
do
sed -e "s|$old_var|$new_var|g" $file > temp.c
mv -f temp.c $file
echo "Replaced $old_var in file $file"
done

done < $1
---------------------------------------------------------

the problem is say i want to replace PROG with J_E_PROG. normally, it will replace properly in all files.

but when it finds something like C_PROG_DEF, it will replace PROG in this also making it as C_J_E_PROG_DEF .
i don't want this C_PROG_DEF => C_J_E_PROG_DEF conversion.

Can i avoid it? how do i do that? I want only complete word replacement

please help.
Thanks in advance
Gauri
 
Old 12-21-2004, 06:22 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

You can try using word boundaries.

A small warning: Not all versions of sed allow this (for example: sed on AIX 4.3).

Ok, here's how it's done:

sed 's/\<red\>/RED/'

or

sed 's/\bred\b/RED/'

I'm kinda busy atm (work.....) so for more details feed your fav searchengine with: sed word boundary

Hope this helps.
 
Old 12-21-2004, 07:29 AM   #3
gd13
LQ Newbie
 
Registered: Dec 2004
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks! that's so cool....
it works...
 
Old 12-21-2004, 07:33 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
you can use m4

(man m4)

here's what I did:

TEST DATA:
Quote:
billym.primadtpdev>cat /export/home/billym/TEST_DATA
only the first should become JE_PROG
PROG = JE_PROG
X_E_PROG != JE_PROG
_PROG_ != JE_PROG
PROG_X != JE_PROG
the m4 command line: (the -Uindex undefines the (maybe)default definition of index)
Quote:
m4 -Uindex -DPROG=JE_PROG /export/home/billym/TEST_DATA
result is:

Quote:
billym.primadtpdev>m4 -Uindex -DPROG=JE_PROG /export/home/billym/TEST_DATA


only the first should become JE_PROG
JE_PROG = JE_PROG
X_E_PROG != JE_PROG
_PROG_ != JE_PROG
PROG_X != JE_PROG
lovely job!
 
  


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
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 11:58 AM
Using sed to replace a lot of text Wynd Programming 9 10-07-2005 04:47 AM
Replace substring with SED marri Programming 2 07-09-2005 06:18 PM
[sed] replace string? chuanyung Programming 3 03-11-2004 09:42 PM
Sed - suitable to replace CR LF? J_Szucs Programming 3 05-12-2003 07:03 PM


All times are GMT -5. The time now is 12:16 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration