|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Please help me, wasted hrs I need a command that will work on file (xml) and replace multiple occurrence (more than 2 times) Examples 1. '===' 2. '====' 3. '=======' should be replaced by just '==' Note :- single character should be replaced. (= should be replaced by ==) I have used this command:- Code:
sed 's/==*/==/g' example.xml > example.xml But after using this command i found, where single character = is there, it changes into == Below are two for examples:- Before:- Code:
1) <namespace key="109" case="first-letter">Book talk</namespace> 2) st = ''[[Sleeping Murder]]'' | cause = After:- Code:
1) <namespace key=="109" case=="first-letter">Book talk</namespace> 2) st == ''[[Sleeping Murder]]'' | cause == Please help, Regards, Red.
Last edited by bakunin; 06-21-2012 at 07:58 AM.. |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Quote:
If you only want to replace two or more occurences of "=" use: Code:
sed 's/===*/==/g' example.xml > example.xml.result You cannot direct the output of sed to the input file, because that would overwrite and destroy the input file after the first line. Therefore a different output file name. I hope this helps. bakunin |
| The Following User Says Thank You to bakunin For This Useful Post: | ||
RedRocks!! (06-21-2012) | ||
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
Thanks you so much bakunin :)
Is there any possibility, that after editing original file, and after process completes, can we replace original with output file, Becoz i have a file of 38 Gb. I have managed shell script, as i am doing shell scripting first time, i am trying to create a script where file should be deleted after completing the process. I want to extend command and delete original file and name the new file created same as old one. Ex. Code:
sed 's/===*/==/g' inputfile.xml > outputfile.xml && COMMAND_FOR_DELETING_OLD_FILE && \ COMMAND_FOR_RENAMING_NEW_FILE_TO_SAME_AS_OLD_FILE -Red. (Becoz which i am working is 38 Gb and my Server dont have much resources, right command will help me.)
Last edited by vbe; 06-22-2012 at 11:06 AM.. Reason: code tags please! |
|
#4
|
|||
|
|||
|
There is no other way than what you have already found out: use a different file and then move the new file over the old file.
The reason for this i have described here and in some other postings too - i don't want to repeat it. Btw., a suggestion: stay away from GNU-seds "-i" switch: it will just make your script less portable, but will do nothing else than do the "mv"-operation automatically afterwards. I hope this helps. bakunin |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
I got another command which is working Great !!
Hello, Thank you for your suggestion. I have used, Below command, where it gives output in same file. Code:
perl -i -pe's/===*/==/g' Example.xml Once again thank you for helping ![]() -Red |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Quote:
You gain nothing by using perl (save for a considerably slower speed of execution, because native UNIX commands are way faster). The point is simple: to edit a file you need to be able to store 2 versions of it.*) There is no way around that. The "-i" options of various tools just blur that fact by hiding this temporary file, but it is still necessary. If you fear a long execution time for moving the file: don't. It is in fact just a change in the files i-node (which is a few bytes) as long as the temporary file and the original file are on the same filesystem. To execute Code:
mv /path/to/fileA /path/to/fileB takes the same time, regardless of the size of this file (as long as they both are on the same filesystem). So set your "TMP" or "TMPDIR" variable accordingly and have enough room on your disk - some 100GB should not really be a problem these times of multi-TB SAN storage fabrics. I hope this helps. bakunin ________________ *) actually this is not completely true, because there is a trick: Code:
(cat /path/to/file) | sed '<somecommand>' > /path/to/file This will work for files which are small enough to fit into memory. The downside is, that if anything goes wrong (power loss, reboot, process aborted, ...) your data will be irrevocably destroyed. You sure do not want to use this hack on critical data just to save a few GB of (temporary) diskspace. Last edited by bakunin; 06-24-2012 at 05:51 PM.. |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Quote:
Regards, Alister |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| get all the strings after the equal sign | h0ujun | Shell Programming and Scripting | 1 | 02-13-2012 10:11 PM |
| search and replace the last occurance of a match in a file | harikris614 | Shell Programming and Scripting | 3 | 07-08-2009 10:34 PM |
| Sed command to globally replace xth occurance. | lowmaster | Shell Programming and Scripting | 14 | 06-15-2009 01:42 PM |
| replace the n'th occurance in a file | subin_bala | UNIX for Dummies Questions & Answers | 2 | 06-04-2008 10:05 AM |
| String search - Command to find second occurance | saurabhsinha23 | UNIX for Dummies Questions & Answers | 5 | 12-06-2007 07:03 PM |
|
|