XML id replacement with shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML id replacement with shell
# 1  
Old 01-24-2013
XML id replacement with shell

Hi..

I have a problem with replacing of
Code:
id-0f5435080b

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

and tomorrow it may be
Code:
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.

Code:
<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

Code:
<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>

I have attached one of my xml files here.

Last edited by nex_asp; 01-24-2013 at 03:38 AM..
# 2  
Old 01-24-2013
hey,

use the below sed command.

Code:
sed -e "s/id-.* name\=\"/my_datafile  name\=\"/g" -e "s/-id-.*\>/-my_datafile /g" -e "s/<\/id-.*/<\/my_datafile >/g" $file > $output


Last edited by Scott; 01-24-2013 at 03:29 AM.. Reason: Code tags, please...
# 3  
Old 01-24-2013
Code:
<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

Code:
<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

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

below one is needed
Code:
</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
Code:
  id-abcdefgf3b

# 4  
Old 01-24-2013
if the units are equl to 10 after id- then you can use below code.

Code:
sed "s/id-........../my_datafile/g" $file > output


Last edited by Scott; 01-24-2013 at 05:12 AM.. Reason: Code tags
This User Gave Thanks to srinivas matta For This Post:
# 5  
Old 01-24-2013
Thanks srinivas matta, if I have to run
Code:
my 1st script daily at 10.00 am

and another script
Code:
weekly once say 11.00 am

and one more script monthly once
Code:
 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

Code:
<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
Code:
<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 ----------

Code:
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 ----------

Code:
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.....

Last edited by nex_asp; 01-24-2013 at 07:34 AM..
# 6  
Old 01-24-2013
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:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. Shell Programming and Scripting

XML data replacement

I have below lines in a file <SubRecord> <Property Name="Name">QQQQQQQQ</Property> <Property Name="Prompt">YYYYYYYYYY</Property> <Property Name="Default">$ddd</Property> <Property Name="HelpTxt">blahblah</Property> <Property Name="ParamType">4</Property> <Property... (8 Replies)
Discussion started by: ratheeshjulk
8 Replies

3. Shell Programming and Scripting

How to add Xml tags to an existing xml using shell or awk?

Hi , I have a below xml: <ns:Body> <ns:result> <Date Month="June" Day="Monday:/> </ns:result> </ns:Body> i have a lookup abc.txtt text file with below details Month June July August Day Monday Tuesday Wednesday I need a output xml with below tags <ns:Body> <ns:result>... (2 Replies)
Discussion started by: Nevergivup
2 Replies

4. Shell Programming and Scripting

Shell Command to compare two xml lines while ignoring xml tags

I've got two different files and want to compare them. File 1 : HTML Code: <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record... (1 Reply)
Discussion started by: Shaishav Shah
1 Replies

5. HP-UX

XML tag name content replacement

Hi, Need to replace an XML tag name contents, please provide any suggestions. Scenario is : <abc_def>Value_some_content</abc_def> Expected output : <abc:def>Value_some_content</abc:def> We have many tag with different names & contents in a file or a string. Please help on the... (3 Replies)
Discussion started by: periyasamycse
3 Replies

6. Shell Programming and Scripting

Scripting question, replacement in xml file

Hi Anybody can help me to make a script that replace string "Su saldo es" with "Your balance" in XML block if it start with Text Id=98 and Text Id= 12 only sample of part of file below <Text Id="98"> <Language id="1">Su saldo es $mainAccountBalance1Tiene ademas $dedicatedAccount1Balance1... (13 Replies)
Discussion started by: Ashu_099
13 Replies

7. Shell Programming and Scripting

XML tag replacement from different XML file

We have 2 XML file 1. ORIGINAL.xml file and 2. ATTRIBUTE.xml files, In the ORIGINAL.xml we need some modification as <resourceCode>431048</resourceCode>under <item type="Manufactured"> tag - we need to grab the 431048 value from tag and pass it to database table in unix shell script to find the... (0 Replies)
Discussion started by: balrajg
0 Replies

8. Shell Programming and Scripting

Problems on cat and substring replacement, shell

Hi I'm new in the Shell world, and I've been trying the whole day to get my script to work. I'm under Ubuntu 10.04. Let me explain: I want to list all the files matching some regex in a directory, and then make some some string replacement on the name of the files, and then a cat on the... (2 Replies)
Discussion started by: albh
2 Replies

9. Shell Programming and Scripting

sed xml file multiple line replacement

I have a file called config.xml, it's a simple xml file, and I need use sed/awk to erase some lines. <machine xsi:type="unix-machineType"> <name>server1</name> <node-manager> <name>server1</name> <listen-address>server1</listen-address> </node-manager> ... (3 Replies)
Discussion started by: cbo0485
3 Replies

10. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies
Login or Register to Ask a Question