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
Can someone help me with a script to replace a particular string in a file?
Thanks and Regards,
karthik
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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).
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
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?
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.
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)
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
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
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-st ring/g
did waht I wanted.
Regards,
Karthik
Well.The following worked fo r me and served the purpose.
Within the vi Editor
at the exit prompt ie., :%s/source-string/Desti-st
did waht I wanted.
Regards,
Karthik
karthik_selamel,
You have the following questions still open.
https://www.experts-exchange.com/questions/20400841/DNS-on-Solaris-and-Intel.html
https://www.experts-exchange.com/questions/20397905/Network-Bandwidth-Measuring-tools.html
https://www.experts-exchange.com/questions/20394906/DNS-syslog-warning.html
https://www.experts-exchange.com/questions/20393324/Script-to-replace-string-in-a-file-Urgent-pls.html
https://www.experts-exchange.com/questions/20392156/Adding-Patches.html
https://www.experts-exchange.com/questions/20386719/CPU-bottleneck.html
https://www.experts-exchange.com/questions/20386717/iostat-output-diagnosis.html
https://www.experts-exchange.com/questions/20386715/Diagnosing-vmstat-output.html
https://www.experts-exchange.com/questions/20386714/Diagnosing-top-output.html
https://www.experts-exchange.com/questions/20385674/Sparc-Hardware-Spec-for-setting-up-DNS-Server.html
https://www.experts-exchange.com/questions/20372145/Package-to-be-installed-for-Netscape-Navigator.html
https://www.experts-exchange.com/questions/20400844/Setting-up-Linus-based-DNS-sesrver.html
It is your responsibility to maintain your open questions by closing them as soon as you get a reasonable answer.
Please consider this letter a gentle reminder that having 12 of 17 questions open is too many, and that Admin has been notified.
Netminder
CS Moderator
You have the following questions still open.
https://www.experts-exchange.com/questions/20400841/DNS-on-Solaris-and-Intel.html
https://www.experts-exchange.com/questions/20397905/Network-Bandwidth-Measuring-tools.html
https://www.experts-exchange.com/questions/20394906/DNS-syslog-warning.html
https://www.experts-exchange.com/questions/20393324/Script-to-replace-string-in-a-file-Urgent-pls.html
https://www.experts-exchange.com/questions/20392156/Adding-Patches.html
https://www.experts-exchange.com/questions/20386719/CPU-bottleneck.html
https://www.experts-exchange.com/questions/20386717/iostat-output-diagnosis.html
https://www.experts-exchange.com/questions/20386715/Diagnosing-vmstat-output.html
https://www.experts-exchange.com/questions/20386714/Diagnosing-top-output.html
https://www.experts-exchange.com/questions/20385674/Sparc-Hardware-Spec-for-setting-up-DNS-Server.html
https://www.experts-exchange.com/questions/20372145/Package-to-be-installed-for-Netscape-Navigator.html
https://www.experts-exchange.com/questions/20400844/Setting-up-Linus-based-DNS-sesrver.html
It is your responsibility to maintain your open questions by closing them as soon as you get a reasonable answer.
Please consider this letter a gentle reminder that having 12 of 17 questions open is too many, and that Admin has been notified.
Netminder
CS Moderator
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 !
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
SpideyMod
Community Support Moderator @Experts Exchange
cat myfile | sed 's/name/MyName/g' > myfile