Link to home
Start Free TrialLog in
Avatar of karthik_selamel
karthik_selamel

asked on

Script to replace string in a file-Urgent pls

Hi,

 Can someone help me with a script to replace a particular string in a file?

Thanks and Regards,

karthik
ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yuzh
yuzh

Let's say you want to relpace "name" with "MyName" in a file myfile:

cat myfile | sed 's/name/MyName/g' > myfile
yzuh,

Your method will work with small files, but you can get into trouble if the file is of significant size. What happens then is that sed starts writing to the file as you read it and maddness can result (you can probably figure out how I found that out).
You are correct jlevie,

it should be something like:
sed 's/name/MyName/g' myfile > myfile.tmp
mv myfile.tmp myfile

jlevie, yuzh,
I use the
 (cat file)| prog > file
method since years, mainly on SunOS and Solaris (never failed for me), but I know that on Linux the limit is around 8k, while some other UNIX (IRIX, AIX) may fail at all.
I'm aware that the problem is somehwere in the combination of CPU&OS&shell, but never got completely used to it.
Does anybody have more information or docs about it?
Yeah, I'd used the simple form for years on Solaris (and SunOS) until I got bit by it one day with a many-multi-meg file. I suspect that it may not be a simple limit on Solaris, but may be influenced by the system resources. And yes Linux does have a much lower limit.

Since then I've made it a practice to always move the input file to a different name and write the output to the original file name.
haha, that's why I like it on SunOS for multi-mega-meg files and no space left on device and no perl ;-)
but the draegon always hits me on other systems ...
(practice is the save way now: mv or perl, 'cause of huge disks, but the ancient systems are still running)
Sometime a sigle file could be  a few GB, it will fail, if you don't use tmp file. The Engerring simulation log file could be huge,the same thing with some of the C-ISAM index file)
yuzh,
as I said: I'm aware that it fails on most systems, but I'm doing it since the time of dawn on SunOS (3.x) SPARC for multi-100meg files, and it never failed.
Once, in a long night, I debugged the bahaviour, and it seems that on SPARC CPUs the reason might be the special register mapping (input vs. output register window) when switching between functions, 'couse the seek pointer is in a register and seems to be a physical (disk?) address.

I'm just curious how it works in detail, sometimes, somewhere
Avatar of karthik_selamel

ASKER

Hi all,

  Well.The following worked fo r me and served the purpose.

 Within the vi Editor

at the exit prompt ie., :%s/source-string/Desti-string/g

did waht I wanted.

Regards,

Karthik
No comment has been added lately, so it's time to clean up this Topic Area.
I will leave a recommendation for this question in the Cleanup topic area as follows:

- Answered by jlevie

Please leave any comments here within the next 7 days

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange