LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-26-2009, 02:09 AM   #1
EdrickV
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Rep: Reputation: 0
Question Multi-line find/replace with GnuWin32 Sed Question


I would like to have a script that will do a search in an XML file for a specific block of XML code and replace it with a different block of XML code using the Windows build of GNU Sed 4.1.5.

Here's the text I want to find:
Code:
      <LibraryObject
         name="{7f38bfbc-e295-4a40-845c-3f3c872cfa82}"
         scale="1.00"
         />
   </SceneryObject>
And here's what I want to replace it with:
Code:
      <LibraryObject
         name="{7f38bfbc-e295-4a40-845c-3f3c872cfa82}"
         scale="1.00"
         />
      <AttachedObject
         attachpointName="attachpt_beacon"
         pitch="0"
         bank="0"
         heading="0">
         <RandomAttach
            randomness="ALWAYS_DISPLAY"
            probability="1"/>
         <Beacon
            type="CIVILIAN"
            baseType="AIRPORT"/>
      </AttachedObject>
   </SceneryObject>
There is however a lot of stuff in there that would mess up sed, and the one example from the sed FAQ (from the Sourceforge site) that looked promising and easy enough to figure out (4.23.3. Try to use a block of "literal strings") fails with this error message:
sed: file blockrep.sed line 18: unterminated `s' command


Anyone got any ideas, or even an alternate program that can do this? (Preferably one that I can automate.)
 
Old 04-26-2009, 03:30 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
with the shell/awk
Code:
#!/usr/bin/bash
string='
      <AttachedObject
         attachpointName="attachpt_beacon"
         pitch="0"
         bank="0"
         heading="0">
         <RandomAttach
            randomness="ALWAYS_DISPLAY"
            probability="1"/>
         <Beacon
            type="CIVILIAN"
            baseType="AIRPORT"/>
      </AttachedObject>"'
awk -v s="$string" '/<\/SceneryObject>/{ print s }1' file
if you prefer Perl
Code:
#!/usr/bin/perl
my $string = <<EOF;
      <AttachedObject
         attachpointName="attachpt_beacon"
         pitch="0"
         bank="0"
         heading="0">
         <RandomAttach
            randomness="ALWAYS_DISPLAY"
            probability="1"/>
         <Beacon
            type="CIVILIAN"
            baseType="AIRPORT"/>
      </AttachedObject>
   </SceneryObject>
EOF
while (<>){ print /<\/SceneryObject>/ ? $string :$_; }
or Python
Code:
#!/usr/bin/env python
string='''
      <AttachedObject
         attachpointName="attachpt_beacon"
         pitch="0"
         bank="0"
         heading="0">
         <RandomAttach
            randomness="ALWAYS_DISPLAY"
            probability="1"/>
         <Beacon
            type="CIVILIAN"
            baseType="AIRPORT"/>
      </AttachedObject>"'''
      
for line in open("file"):
    line=line.strip()
    if "SceneryObject>" in line:
        print string
    print line
otherwise, for more complex XML processing, use a XML parser. eg xgawk, XML::Parser(Perl) ,or xml (Python)
 
Old 04-26-2009, 04:46 AM   #3
EdrickV
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Original Poster
Rep: Reputation: 0
The awk script (which I had to run in Cygwin since I am using Windows not Linux) does not work quite right because there is more then one SceneryObject in each file and only one of them in each file I'm working on needs to be changed. (The actual object would be identified using the {7f38bfbc-e295-4a40-845c-3f3c872cfa82} bit which is unique to that object.) I was really hoping for something using sed since I can run that outside of Cygwin, but I'm still not really sure if the GnuWin32 sed is capable of that.
 
Old 04-26-2009, 05:00 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
awk -v s="$string" '/7f38bfbc-e295-4a40-845c-3f3c872cfa82/{got=1}/<\/SceneryObject>/ && got{ print s;got=0 }1' file
 
Old 04-26-2009, 01:32 PM   #5
EdrickV
LQ Newbie
 
Registered: Nov 2006
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks. That works a bit better. Though I did have to remove some double quotes from the end of the replacement string. Automating it took a little more work and a second script though since awk doesn't have an in-place edit option like sed does. But I'll take what I can get and this does the job. In case anyone wonders what the final scripts look like, here they are.

beconfix:
Code:
#!/bin/sh
bfix2 3AK1_ADE_HC.xml
bfix2 55AK_ADE_HC.xml
bfix2 A89_ADE_HC.xml
bfix2:
Code:
#!/usr/bin/bash
string='      <AttachedObject
         attachpointName="attachpt_beacon"
         pitch="0"
         bank="0"
         heading="0">
         <RandomAttach
            randomness="ALWAYS_DISPLAY"
            probability="1"/>
         <Beacon
            type="CIVILIAN"
            baseType="AIRPORT"/>
      </AttachedObject>'
awk -v s="$string" '/7f38bfbc-e295-4a40-845c-3f3c872cfa82/{got=1}/<\/SceneryObject>/ && got{ print s;got=0 }1' $@ > temp.xml
cp temp.xml $@
u2d $@
rm temp.xml
 
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed - How do you replace end of line with a space pppaaarrrkkk Programming 7 02-07-2011 11:27 AM
sed multi-line search/replace woes djmm Programming 8 03-17-2009 05:25 AM
search and replace with multi line string kambrish Programming 5 04-28-2008 06:02 AM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM
sed: replace one line with >one line bbeers Programming 3 11-19-2002 05:27 PM


All times are GMT -5. The time now is 11:26 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration