Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update a XML file using Korn Shell

Status
Not open for further replies.

kre1973

IS-IT--Management
May 5, 2006
47
US
I have the following XML file called: Today.xml in which I want to insert the current time in the node called: <TODAY>.

Code:
<?xml version="1.0" standalone="yes"?>
<MSR>
  <Info>
    <TEEFHR />
    <TEPP />
    <TNC />
    <TODAY />
    <UPDATE />
    <UMZ />
    <UMZT />
    </Info>
</MSR>

thanks
 
I guess using AWK or SED would be fine also.
 
It works, but I need to save the updated XML file.
 
Hi

If your [tt]sed[/tt] supports in-place editing, then :
Code:
sed [red]-i[/red] "s!<TODAY />!<TODAY>$( date )</TODAY>!" /input/file
Otherwise you can do "manually" what [tt]sed[/tt] does when using -i :
Code:
tempfile="$( mktemp '/tmp/sedXXXXXX' )"
sed "s!<TODAY />!<TODAY>$( date )</TODAY>!" "/input/file" > "$tempfile"
chown --reference="/input/file" "$tempfile"
chmod --reference="/input/file" "$tempfile"
mv "$tempfile" "/input/file"
Of course, your [tt]chown[/tt] and [tt]chmod[/tt] may not support the --reference option, or the [tt]mktemp[/tt] may have different syntax. So in fewer words :
Code:
sed "s!<TODAY />!<TODAY>$( date )</TODAY>!" "/input/file" > "/output/file"

Feherke.
 
Rather than chowning and stuff I would typically use cp -p to preserve ownership, timestamp and permissions, e.g.

Code:
cp -p filename.xml filename.xml.orig
sed "s!<TODAY />!<TODAY>$( date )</TODAY>!" filename.xml.orig > filename.xml


Annihilannic.
 
Hi

I avoided [tt]cp[/tt] because the file size is unknown and copying could add speed and storage problem. ( I think this is why neither [tt]sed[/tt] does the copying. )

Anyway, your suggested way has a good point too, it preserves the inode.

But also raises a question : why the -p ?

Feherke.
 
Annihilannic said:
... I would typically use cp -p to preserve ownership, timestamp and permissions, e.g.

I like the backed up .orig file to have the timestamp unchanged, so I know when it was previously changed or created...

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top