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

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

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!

#!/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<&-

You can try something like that :

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.

I may be old, but works nicely. Jerardfjay

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 for the forum!

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.