Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8144711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:27:28+00:00 2026-06-06T13:27:28+00:00

I have a file test.xml with the following content : <body> <content> <p>Lorem ipsum

  • 0

I have a file test.xml with the following content :

<body>
<content>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam.</p><p>Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi,..</p><p>
<MEDIAREF localid="HTM37c2ae34-b92c-11e1-86ab-e6b6e8e434a7-0"/>
</p><p>Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue</p><p>
<MEDIAREF localid="HTM37c2ae34-b92c-11e1-86ab-e6b6e8e434a7-1"/>
</p><p>Praesent egestas leo in pede. Praesent blandit odio eu enim.
</p>
</content>
</body>
...
<ZONEMEDIAS>
<MEDIA localid="HTM37c2ae34-b92c-11e1-86ab-e6b6e8e434a7-0">
<MEDIAPROPRIETES>
<PROPRIETE value="HTM" name="type"/>
</MEDIAPROPRIETES>
<CODEMEDIA>&lt;object width="493" height="370"&gt;&lt;param name="movie" value="http://www.youtube.com/v/Rxxxxxfr_FR&amp;amp;rel=0"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/RxxxxxfrR&amp;amp;rel=0" type="application/x-shockwave-flash" width="493" height="370" allowscriptaccess="always" allowfullscreen="true"&gt;&lt;/embed&gt;&lt;/object&gt;
</CODEMEDIA>
</MEDIA><MEDIA localid="HTM37c2ae34-b92c-11e1-86ab-e6b6e8e434a7-1">
<MEDIAPROPRIETES>
<PROPRIETE value="HTM" name="type"/>
</MEDIAPROPRIETES>
<CODEMEDIA>&lt;blockquote class="twitter-tweet" lang="fr"&gt;&lt;p&gt;second texte to replace &lt;a href="https://twitter.com/xxxx" data-datetime="2012-06-15T01:12:03+00:00"&gt;Juin 15, 2012&lt;/a&gt;&lt;/blockquote&gt;
&lt;script src="//platform.twitter.com/widgets.js" charset="utf-8"&gt;&lt;/script&gt;
</CODEMEDIA>
</MEDIA>
</ZONEMEDIAS>

I expected replace

<MEDIAREF localid="HTM37c2ae34-b92c-11e1-86ab-e6b6e8e434a7-0"/>

by

&lt;object width="493" height="370"&gt;&lt;param name="movie" value="http://www.youtube.com/v/Rxxxxxfr_FR&amp;amp;rel=0"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/RxxxxxfrR&amp;amp;rel=0" type="application/x-shockwave-flash" width="493" height="370" allowscriptaccess="always" allowfullscreen="true"&gt;&lt;/embed&gt;&lt;/object&gt;

and do the same for ::

Is there any way to achieve this in bash please ?

I tried something like this to retrieve value if localid

grep "<MEDIAREF localid=.*\".>" test.xml | sed -e "s/^.*<MEDIAREF localid=/<MEDIAREF localid=/"  | cut -f2 -d"\"" | cut -f1 -d"\""

but I don’t figure out how to replace after

Someone can help me please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:27:29+00:00Added an answer on June 6, 2026 at 1:27 pm

    In bash

    while read line;do
      if [[ $line =~ 'MEDIAREF localid="HTM37c2ae34-b92c-11e1-86ab-e6b6e8e434a7-0"' ]];then
        echo '&lt;object width="493" height="370"&gt;&lt;param name="movie" value="http://www.youtube.com/v/Rxxxxxfr_FR&amp;amp;rel=0"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/RxxxxxfrR&amp;amp;rel=0" type="application/x-shockwave-flash" width="493" height="370" allowscriptaccess="always" allowfullscreen="true"&gt;&lt;/embed&gt;&lt;/object&gt;';
      else
        echo "$line";
      fi;
    done  < test.xml > new-test.xml
    

    EDIT, After your comment I understand better what you want to do, bash is not the best solution and may not be safe, perl is better, but it works with your example. Here a solution with bash :

    #!/bin/bash
    # First loop to set variables ${!HTM*}
    localid=""         # localid property
    codemedia=0        # flag to indicate between CODEMEDIA tags
    cm_content=""      # CODEMEDIA content
    while IFS=\< read -d \> content tag;do
        if((codemedia==1));then
            cm_content="$cm_content$content"
        fi
        # set the flag or localid property
        if [[ "$tag" =~ ^CODEMEDIA.* ]];then
            codemedia=1
        elif [[ "$tag" =~ ^/CODEMEDIA ]];then
            codemedia=0
            eval "$localid=\"${cm_content//\"/\\\"}\""
            cm_content=""
        elif [[ "$tag" =~ ^MEDIA\  ]];then
            lf=0
            while read -d \" lprop;do
                if((lf==1));then
                    localid=${lprop//-/_}
                    break
                fi
                if [[ "$lprop" =~ localid=$ ]];then
                    lf=1
                fi
            done <<<$tag
        fi
    done < test.xml
    echo ${!HTM*}
    # Second loop to replace MEDIAREF tag
    { while IFS=\< read -d \> content tag;do
        if [[ "$tag" =~ ^MEDIAREF.*/$ ]];then
            lf=0
            while read -d \" lprop;do
                if((lf==1));then
                    localid=${lprop//-/_}
                    break
                fi
                if [[ "$lprop" =~ localid=$ ]];then
                    lf=1
                fi
            done <<<$tag
            echo -n "$content${!localid}"
        else
            echo -n "$content<$tag>"
        fi
    done;echo;} < test.xml > new-test.xml
    

    Other solution in perl:

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my $inputFile=$ARGV[0]||"test.xml";
    my %hash;
    open(INPUT,"<$inputFile") or die "cannot open $inputFile for readding";
    # reads whole file in $_ (see perlvar)
    # other solution if file is too long is to set $/="</MEDIA>"; for example and to change the while loop
    $_=join("",<INPUT>);
    # for gms flags (see perlre)
    while(m{<MEDIA localid="(.*?)".*?<CODEMEDIA>(.*?)</CODEMEDIA>.*?</MEDIA>}gms){
        $hash{$1}=$2;
    }
    close(INPUT);
    open(INPUT,"<$inputFile") or die "cannot open $inputFile for readding";
    # again reads whole file
    $_=join("",<INPUT>);
    s{<MEDIAREF localid="(.*?)".*?/>}{$hash{$1}}gms;
    close(INPUT);
    # print on STDOUT
    # other solution open(OUTPUT,">filename"); print OUTPUT $_; close(OUTPUT);
    print;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following xml file test.xml: <a> <b c=test1 d=test2/> <b c=test3 d=test4/>
I have the following xml file: <?xml version=1.0 encoding=utf-8?> <root> <phrase id=test><![CDATA[test]]></phrase> <phrase id=test0><![CDATA[test0]]></phrase>
Lets assume a xml file named data.xml with following content: <root> <record> <id>1</id> <name>test
I have an XML file content similar to the following: <?xml version=1.0 encoding=utf-8?> <Content>
I have the following TextView in my XML layout file:- <TextView android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/autolink_test
I have an xml file that contains its element like <ab:test>Str</ab:test> When I am
I have this test code that just saves an XML file to a folder.
I have this inside my spring xml file: <object type=Test.Web.Utilities.TestClass, Test.Web singleton=false id=TestClass> <property
i'm using Spring-WS and have the following spring-ws-servlet.xml file. The injection of defaultURI and
In my \drawable-mdpi folder, I have an image named: test.jpg In my main.xml file,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.