Go Back   The UNIX and Linux Forums > Operating Systems > Linux


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-21-2012
Registered User
 
Join Date: Jun 2012
Location: India
Posts: 7
Thanks: 2
Thanked 1 Time in 1 Post
Data Linux command to find and replace occurance of more than two equal sign with "==" from XML file.

Please help me, wasted hrs, to find this soulution:-
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.

Moderator's Comments:
edit by bakunin: Please use CODE-tags when posting code or terminal output.

Corrected typo in thread title.

Last edited by bakunin; 06-21-2012 at 07:58 AM..
Sponsored Links
    #2  
Old 06-21-2012
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 3,303
Thanks: 27
Thanked 459 Times in 358 Posts
Quote:
Originally Posted by RedRocks!! View Post
I need a command that will work on file (xml) and replace multiple occurrence (more than 2 times)

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 ==
Seems to me that this is what you wanted, isn't it?

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  
Old 06-21-2012
Registered User
 
Join Date: Jun 2012
Location: India
Posts: 7
Thanks: 2
Thanked 1 Time in 1 Post
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.)
Moderator's Comments:
Please use next time code tags for your code and data

Last edited by vbe; 06-22-2012 at 11:06 AM.. Reason: code tags please!
    #4  
Old 06-23-2012
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 3,303
Thanks: 27
Thanked 459 Times in 358 Posts
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  
Old 06-24-2012
Registered User
 
Join Date: Jun 2012
Location: India
Posts: 7
Thanks: 2
Thanked 1 Time in 1 Post
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  
Old 06-24-2012
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 3,303
Thanks: 27
Thanked 459 Times in 358 Posts
Quote:
Originally Posted by RedRocks!! View Post
Thank you for your suggestion. I have used, Below command, where it gives output in same file.
You seem to have misunderstood what i explained. "perl -i" does the same as "sed -i": it creates a temporary second file where it stores the changes and moves this over the original file as the last step.

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  
Old 06-24-2012
alister alister is offline Forum Advisor  
Registered User
 
Join Date: Dec 2009
Posts: 2,607
Thanks: 123
Thanked 719 Times in 602 Posts
Quote:
Originally Posted by bakunin View Post
... 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.
That won't work even when there's just a single byte in the file, if the shell first creates the sed portion of the pipeline. There are no guarantees on which component of a pipeline will be created first.

Regards,
Alister
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 03:41 AM.