# script to run shell command and insert results to existing xml file

**URL:** <https://community.unix.com/t/script-to-run-shell-command-and-insert-results-to-existing-xml-file/155344>\
**Category:** Shell Programming and Scripting\
**Created:** [August 11, 2005, 7:26pm UTC](https://community.unix.com/t/script-to-run-shell-command-and-insert-results-to-existing-xml-file/155344 "2005-08-11T19:26:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![littlejon](https://community.unix.com/letter_avatar/littlejon/32/6_22734026db12803ebb3e059622ac3534.png) [@littlejon](https://community.unix.com/u/littlejon)\
**Post date:** [August 11, 2005, 7:26pm UTC](https://community.unix.com/t/script-to-run-shell-command-and-insert-results-to-existing-xml-file/155344/1 "2005-08-11T19:26:12Z")

</div>

Hi. Thanks for any help with this. I'm not new to programming but I am new to shell programming. I need a script that will

1. execute 'df -k' and return the volume names with specific text
2. surround each line of the above results in opening and closing xml tags
3. insert the results of step #2 into an existing file after a specific opening xml tag.

Any help would be greatly appreciated!

littlejon 😕

---

<div class="post-metadata">

**Author:** ![Ygor](https://community.unix.com/user_avatar/community.unix.com/ygor/32/104_2.png) [@Ygor](https://community.unix.com/u/Ygor)\
**Post date:** [August 11, 2005, 9:36pm UTC](https://community.unix.com/t/script-to-run-shell-command-and-insert-results-to-existing-xml-file/155344/2 "2005-08-11T21:36:43Z")

</div>

I have this old script ksh script which takes the output from df and produces a html page. It is specific to HP-UX (hence the "df -Pkl") but you could adapt it to your needs. You probably don't need the bar graph showing disk capacity!

```nohighlight
#!/usr/bin/ksh
exec 3> df.html
print -u3 '<HTML><HEAD><TITLE>STATS</TITLE></HEAD><BODY>'
print -u3 '<H3>'$(uname -n)'</H3><TABLE BORDER=2 CELLSPACING=0 CELLPADDING=2>'
print -u3 '<TR><TH>Filesystem</TH><TH>1024-blocks</TH><TH>Used</TH>'
print -u3 '<TH>Available</TH><TH>Capacity</TH><TH>Mounted On</TH></TR>'
df -Pkl |awk 'NR>1 && NF==6'|sort|while read FS SIZE USED FREE PERC MOUNT
do
  print -u3 '<TR><TD>'$FS'</TD><TD ALIGN=RIGHT>'$SIZE'</TD>'
  print -u3 '<TD ALIGN=RIGHT>'$USED'</TD><TD ALIGN=RIGHT>'$FREE'</TD>'
  print -u3 '<TD><TABLE BORDER=0 CELLSPACING=1 CELLPADDING=0><TR>'
  print -u3 '<TD WIDTH='$((150*$USED/$SIZE))' BGCOLOR="black"></TD>'
  print -u3 '<TD WIDTH='$((150*$FREE/$SIZE))' BGCOLOR="gray"></TD>'
  print -u3 '<TD><FONT SIZE=-1 COLOR=black> '$PERC'</FONT></TD>'
  print -u3 '<TR></TABLE></TD><TD>'$MOUNT'</TD></TR>'
done
print -u3 '</TABLE><P>Generated at '$(date '+%H:%M on %d-%b-%y')'</BODY></HTML>'
exec 3<&-

```

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [August 12, 2005, 2:18am UTC](https://community.unix.com/t/script-to-run-shell-command-and-insert-results-to-existing-xml-file/155344/3 "2005-08-12T02:18:10Z")

</div>

You can try something like that :

```nohighlight
result_file=df.dat

open_tag='<TAG>'
close_tag='</TAG>'
insert_point='<XML>'

df_file=/tmp/$$.tmp
temp_file=/tmp/$$.tmp

# Build lines to be inserted in result file
df -k | tail +2 | while read line
do
   echo "${open_tag}\n${line}\n${close_tag}"
done >> ${df_file}

# Insert lines in result file
sed -e "/${insert_point}/r ${df_file}" ${result_file} > ${temp_file}
mv ${temp_file} ${result_file}

```

Jean-Pierre.

---

<div class="post-metadata">

**Author:** ![jerardfjay](https://community.unix.com/letter_avatar/jerardfjay/32/6_22734026db12803ebb3e059622ac3534.png) [@jerardfjay](https://community.unix.com/u/jerardfjay)\
**Post date:** [August 12, 2005, 1:56pm UTC](https://community.unix.com/t/script-to-run-shell-command-and-insert-results-to-existing-xml-file/155344/4 "2005-08-12T13:56:46Z")

</div>

> [@ygor](#):
>
> I have this old script ksh script which takes the output from df and produces a html page. It is specific to HP-UX (hence the "df -Pkl") but you could adapt it to your needs.

I may be old, but works nicely. Jerardfjay

---

<div class="post-metadata">

**Author:** ![littlejon](https://community.unix.com/letter_avatar/littlejon/32/6_22734026db12803ebb3e059622ac3534.png) [@littlejon](https://community.unix.com/u/littlejon)\
**Post date:** [August 12, 2005, 4:57pm UTC](https://community.unix.com/t/script-to-run-shell-command-and-insert-results-to-existing-xml-file/155344/5 "2005-08-12T16:57:24Z")

</div>

Thanks to both Ygor and Aigles. I went with Aigles' suggestion and it worked like a charm. Of course, I tweaked it to meet my needs. I was originally trying to use standard programming techniques to solve this problem. I am now officialy amazed at the power of sed and shell programming. Thank you, again! And, thanks to [unix.com](http://unix.com) for the forum!

---

<div class="post-metadata">

**Author:** ![littlejon](https://community.unix.com/letter_avatar/littlejon/32/6_22734026db12803ebb3e059622ac3534.png) [@littlejon](https://community.unix.com/u/littlejon)\
**Post date:** [August 12, 2005, 4:59pm UTC](https://community.unix.com/t/script-to-run-shell-command-and-insert-results-to-existing-xml-file/155344/6 "2005-08-12T16:59:53Z")

</div>

Thanks, jerardfjay. Your post must have just made it while I was typing my last one. I think I've got the problem solved. I appreciate your suggestion, though. littlejon.
