Linux command to find and replace occurance of more than two equal sign with "==" from XML file.


 
Thread Tools Search this Thread
Operating Systems Linux Linux command to find and replace occurance of more than two equal sign with "==" from XML file.
# 1  
Old 06-21-2012
Data Linux command to find and replace occurance of more than two equal sign with "==" from XML file.

Please help me, wasted hrsSmilie, 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:
Mod Comment 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 08:58 AM..
# 2  
Old 06-21-2012
Quote:
Originally Posted by RedRocks!!
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
This User Gave Thanks to bakunin For This Post:
# 3  
Old 06-21-2012
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:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 06-22-2012 at 12:06 PM.. Reason: code tags please!
# 4  
Old 06-23-2012
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
# 5  
Old 06-24-2012
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 Smilie

-Red
# 6  
Old 06-24-2012
Quote:
Originally Posted by RedRocks!!
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 06:51 PM..
# 7  
Old 06-24-2012
Quote:
Originally Posted by bakunin
... 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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I redirect output from "find", either to a file or another command?

I'm trying to find out what happened to the rogue game that apt-get told me it installed, so I thought I would find the file. I went to the root and entered: find -name "rog*.*" I get a large number of lines saying my access is denied in various directories. I figure I'll practice my Unix... (14 Replies)
Discussion started by: arghvark
14 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Ubuntu

How to change "more" to "more or equal" in this line?

Hi, Below line selects only dates comes after than today. I need to change below line as "more or equal": awk -F\## -v d=$(date +%Y%m%d) '{if($NF>d)print}' /usr/batch/pill.txt > /usr/batch/pill_in_use.txt When we change the code, it will print today's date and following dates only. ... (2 Replies)
Discussion started by: baris35
2 Replies

5. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

6. UNIX for Dummies Questions & Answers

replace "," with "." only in specific columns of a file?

Hi all, I have this text file containing 9 columns separated by space. The 8th columns contains the numbers. C1 C2 C3 C4 C5 C6 C7 C8 C9 er rt yt gh iu nk il 0.07 xs yt lr ty bg iu zk nh 0,0005 lt ...etc. I want to replace the comma with full stop only in 8th coloumn. the output... (8 Replies)
Discussion started by: Unilearn
8 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

Interesting question - Search and replace the word after sign "="

Hi Guys, Req your help in searching and replacing the word that comes after equals(=) symbol I would like to replace the sting in bold with a string in variable. d=ABCDF8C44C22 # grep -i NIM_MASTERID ${_NIMINFO} export NIM_MASTERID=00CDF8C44C00 I'm looking to replace any word that... (4 Replies)
Discussion started by: ajilesh
4 Replies

9. Shell Programming and Scripting

"find command" to find the files in the current directories but not in the "subdir"

Dear friends, please tell me how to find the files which are existing in the current directory, but it sholud not search in the sub directories.. it is like this, current directory contains file1, file2, file3, dir1, dir2 and dir1 conatins file4, file5 and dir2 contains file6,... (9 Replies)
Discussion started by: swamymns
9 Replies
Login or Register to Ask a Question