using sed with xml files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using sed with xml files
# 1  
Old 03-16-2006
using sed with xml files

Hello
I'm working on a project modifying XML files in Unix and, I would like to use sed to change these values. For example, I've got a tag <book>354678209<\book> and I want to replace the value 354678209 with a another value without having to go into the file and change it manually. How can I do that using sed?
# 2  
Old 03-16-2006
Code:
sed -e 's_\(<book>\)[0-9]*\(<\\book>\)_\11234567890\2_g'

The number in bold 1234567890 is the new number. It will replace whatever value is found between the book tags.
# 3  
Old 03-16-2006
Of course you could use sed for this task,
as was shown by vino.
But parsing XML with sed seems like auto-flagellation to me.
I surely would recommend Perl to the rescue. Smilie
# 4  
Old 03-16-2006
You could use [gn]awk...
Code:
BEGIN { FS = "[<|>]" }
{
        if ($2 == "book") {
                sub($3, "01234")
        }
        print
}

If you feel the need to use perl, you can feed the above code through a2p...
Code:
$[ = 1;
$FS = '[<|>]';

while (<>) {
    @Fld = split($FS, $_, 9999);
    if ($Fld[2] eq 'book') {
        $s = $Fld[3], s/$s/01234/;
    }
    print $_;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bit of sed help in XML

Hi all, need some help seeing the bug in my SED Source XML <SMART_FOLDER JOBISN="1" SUB_APPLICATION="PMT-APB" MEMNAME="Job0" JOBNAME="PMT-APB" FOLDER_NAME="PMT-APB"> </SMART_FOLDER> My SED Command sed -e 's/\(<SMART_FOLDER \)\(.*FOLDER_NAME="PMT-APB"\)/\FOLDER_ORDER_METHOD="PCI" \2/' <... (0 Replies)
Discussion started by: J-Man
0 Replies

2. Shell Programming and Scripting

Splitting a single xml file into multiple xml files

Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Sample.xml consists multiple headers so how can we split these multiple headers into multiple files in unix. eg : <?xml version="1.0" encoding="UTF-8"?> <ml:individual... (3 Replies)
Discussion started by: Narendra921631
3 Replies

3. Shell Programming and Scripting

Splitting xml file into several xml files using perl

Hi Everyone, I'm new here and I was checking this old post: /shell-programming-and-scripting/180669-splitting-file-into-several-smaller-files-using-perl.html (cannot paste link because of lack of points) I need to do something like this but understand very little of perl. I also check... (4 Replies)
Discussion started by: mcosta
4 Replies

4. Shell Programming and Scripting

Extract strings from XML files and create a new XML

Hello everybody, I have a double mission with some XML files, which is pretty challenging for my actual beginner UNIX knowledge. I need to extract some strings from multiple XML files and create a new XML file with the searched strings.. The original XML files contain the source code for... (12 Replies)
Discussion started by: milano.churchil
12 Replies

5. Shell Programming and Scripting

Compare two xml files while ignoring some xml tags

I've got two different files and want to compare them. File 1 : <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record id="38,557"><columns><column><name>orge... (2 Replies)
Discussion started by: Shaishav Shah
2 Replies

6. Shell Programming and Scripting

Again, Sed and Xml

Hi all, I have a xml file like below, <JavaArgs> <containerSet> <container id="333"> <JavaArgs> Very Big String </Javargs> </container> <container id="334"> ... (5 Replies)
Discussion started by: nasbtv
5 Replies

7. Shell Programming and Scripting

sed and XML

I am trying to edit an XML file with sed, and I can successfully do so, but the resulting file no longer opens as an XML file? I am working on a project to automate DVD authoring. I am using DVD Studio Pro, which uses XML files as its instructions for DVD authoring. I've worked out a... (2 Replies)
Discussion started by: Starcast
2 Replies

8. Shell Programming and Scripting

modifying xml files using sed

Hello, I have lots of xml files in the same format and I need to modify a xml tag in these files. i loop over the files and apply sed to the files to make the modification but CPU goes to %100 while doing this. I think I'm doing something wrong. Here is my onliner: for f in $( find . -name... (1 Reply)
Discussion started by: xyzt
1 Replies

9. Shell Programming and Scripting

SED for XML's

Hi My xml has element like below <BOOK>:</BOOK> If i wanna replace ":</BOOK>" with ": </BOOK>" i.e 3 spaces added. what should be the search pattern? I tried with "?:\<\/BOOK\>", but its not working in SED. Can someone suggest what would be the search patter looks like ? Thanks (1 Reply)
Discussion started by: braindrain
1 Replies

10. Shell Programming and Scripting

using sed with xml files part 2

I'm trying to replace a date in an XML file that has the format mm/dd/yyyy. I'm using the Unix date function to set up a variable with the current date but, when I try to replace the value in the XML file, the error message says it cannot be parsed. Here is the command I'm using ... (2 Replies)
Discussion started by: stonemonolith
2 Replies
Login or Register to Ask a Question