XML id replacement with shell

Hi..

I have a problem with replacing of

id-0f5435080b

with some name daily, problem here is whenever I generate xml file it generates unique id for instance say for example today

id-0f5435080b

and tomorrow it may be

id-0f68643508

so basically I just want to replace this id with some name say my_datafile in file, till date I am doing with find and replace in gedit manually, if any automation is possible to replace this unique id please help.

<datasets>
  <id-0f5435080b name="ICOADS 1-degree Enhanced" url="http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/coads/1degree/global/enh/sflx.mean.nc">
    <variables>
      <sflx-id-0f5435080b name="Sensible Heat Parameter Monthly Mean at Surface" units="degC m/s" url="#sflx">
        <link match="/lasdata/grids/grid-lon-lat-time-id-0f5435080b" />
      </sflx-id-0f5435080b>
    </variables>
  </id-0f5435080b>

requirement of result is as follows

<datasets>
  <my_datafile name="ICOADS 1-degree Enhanced" url="http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/coads/1degree/global/enh/sflx.mean.nc">
    <variables>
      <sflx-my_datafile name="Sensible Heat Parameter Monthly Mean at Surface" units="degC m/s" url="#sflx">
        <link match="/lasdata/grids/grid-lon-lat-time-my_datafile" />
      </sflx-my_datafile>
    </variables>
  </my_datafile>
</datasets>
<grids>
  <grid-lon-lat-time-my_datafile>
    <link match="/lasdata/axes/lon-x-my_datafile" />
    <link match="/lasdata/axes/lat-y-my_datafile" />
    <link match="/lasdata/axes/time-t-my_datafile" />
  </grid-lon-lat-time-my_datafile>
</grids>
<axes>
  <lon-x-my_datafile type="x" units="degrees_east">
    <arange start="0.5" size="360" step="1" />
  </lon-x-my_datafile>
  <lat-y-my_datafile type="y" units="degrees_north">
    <arange start="-89.5" size="180" step="1" />
  </lat-y-my_datafile>
  <time-t-my_datafile type="t" units="month">
    <arange start="1960-01-01" size="636" step="1" />
  </time-t-my_datafile>
</axes>

[/CODE]I have attached one of my xml files here.

hey,

use the below sed command.

sed -e "s/id-.* name\=\"/my_datafile  name\=\"/g" -e "s/-id-.*\>/-my_datafile /g" -e "s/<\/id-.*/<\/my_datafile >/g" $file > $output
<axes>
  <lon-x-my_datafile ">
    <arange start="0.5" size="360" step="1" />
  </lon-x-my_datafile >
  <lat-y-my_datafile ">
    <arange start="-89.5" size="180" step="1" />
  </lat-y-my_datafile >
  <time-t-my_datafile ">
    <arange start="1960-01-01" size="636" step="1" />
  </time-t-my_datafile >
</axes>

see this is output from your code

this is I wanted

<axes>
  <lon-x-my_datafile type="x" units="degrees_east">
    <arange start="0.5" size="360" step="1" />
  </lon-x-my_datafile>
  <lat-y-my_datafile type="y" units="degrees_north">
    <arange start="-89.5" size="180" step="1" />
  </lat-y-my_datafile>
  <time-t-my_datafile type="t" units="month">
    <arange start="1960-01-01" size="636" step="1" />
  </time-t-my_datafile>
</axes>

and more think spacing also results wrong

see below

</sflx-my_datafile >
    </variables>
  </my_datafile >
</datasets>
<grids>
  <grid-lon-lat-time-my_datafile >

below one is needed

</sflx-my_datafile>
    </variables>
  </my_datafile>
</datasets>
<grids>
  <grid-lon-lat-time-my_datafile>

some parts are missing

what I noticed is while replacing id manually after id 10 unique numbers/alphabets are coming that's

  id-abcdefgf3b

if the units are equl to 10 after id- then you can use below code.

sed "s/id-........../my_datafile/g" $file > output
1 Like

Thanks srinivas matta, if I have to run

my 1st script daily at 10.00 am 

and another script

weekly once say 11.00 am

and one more script monthly once

 12.00 am

automatically means, what I need to do.

---------- Post updated at 05:25 AM ---------- Previous update was at 04:38 AM ----------

Sorry again noticed one more problem output coming like this

<grmy_datafileme-my_datafile>
    <link match="/lasdata/axes/lon-x-my_datafile" />
    <link match="/lasdata/axes/lat-y-my_datafile" />
    <link match="/lasdata/axes/time-t-my_datafile" />
  </grmy_datafileme-my_datafile>
</grids>

it was supposed to be

<grids>
  <grid-lon-lat-time-my_datafile>
    <link match="/lasdata/axes/lon-x-my_datafile" />
    <link match="/lasdata/axes/lat-y-my_datafile" />
    <link match="/lasdata/axes/time-t-my_datafile" />
  </grid-lon-lat-time-my_datafile>
</grids>

I think sed won't help from below code got some partial result

---------- Post updated at 05:59 AM ---------- Previous update was at 05:25 AM ----------

sed -e "s/<id-..........\=\"/<my_datafile\=\"/g" -e "s/-id-..........\>/-my_datafile/g" -e "s/<\/id-........../<\/my_datafile>/g"

but still can't trust like find and replace in editor, if any efficient method is available means please post a code.

---------- Post updated at 06:58 AM ---------- Previous update was at 05:59 AM ----------

awk '{ for (i=1; i<=NF; i++) if ($i ~ /id-/) print "field:", i, $i }' file

above one blindly..checks..
how can I search only id-................ and replace with my string.....

You're wanting a whole lot more than replacement, it seems, given you show 8 lines of input and 25 lines of output.

To do just the replacement:

awk '{ gsub("id-"ID, NEWSTR); } 1' ID="12345" newstr="id-67890" inputfile > outputfile

I'll need more detail about where you're getting the rest of the changes you want.