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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to run shell command and insert results to existing xml file
# 1  
Old 08-11-2005
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 Smilie
# 2  
Old 08-11-2005
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!
Code:
#!/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<&-

# 3  
Old 08-12-2005
You can try something like that :
Code:
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.
# 4  
Old 08-12-2005
Quote:
Originally Posted by 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
# 5  
Old 08-12-2005
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!
# 6  
Old 08-12-2005
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.
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 insert subnode in xml file using xmlstarlet or any other bash command?

I have multiple xml files where i want to update a subnode if the subnode project points to different project or insert a subnode if it doesn't exist using a xmlstarlet or any other command that can be used in a bash script. I have been able to update the subnode project if it doesn't point to... (1 Reply)
Discussion started by: Sekhar419
1 Replies

2. UNIX for Beginners Questions & Answers

Grepping multiple XML tag results from XML file.

I want to write a one line script that outputs the result of multiple xml tags from a XML file. For example I have a XML file which has below XML tags in the file: <EMAIL>***</EMAIL> <CUSTOMER_ID>****</CUSTOMER_ID> <BRANDID>***</BRANDID> Now I want to grep the values of all these specified... (1 Reply)
Discussion started by: shubh752
1 Replies

3. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

4. Shell Programming and Scripting

Read xml file till script finds separation and run again for next input and so on

Hi All, I have one query, I managed to run script with user inputs through command line or with 1 file. But I need to read a txt file/xml file in which user can mention multiple sets of answers and script should run for each set till it reach the EOF. Thanks in advance for example, the file... (3 Replies)
Discussion started by: rv_champ
3 Replies

5. 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

6. Shell Programming and Scripting

Insert shell command into first line of output file

How can I insert the command executed on the shell into the first line of my output file? For example if I execute; zcat *.gz |grep “User5501” > users.out How can I make my users.out look like; zcat *.gz |grep “User5501” > users.out User5501 PA User5501 UA User5501 ZA... (3 Replies)
Discussion started by: lewk
3 Replies

7. Shell Programming and Scripting

insert pipes for existing and non-existing records

I have a source file like this, L4058S462 34329094 F51010141TK1070000483L4058S462 34329094 0232384840 381892 182 5690 L4058S462 34329094 F51020141FIRST CLEARING, LLC A/C 3432-9094 L4058S462 34329094 F51030141JOHAN HOLMQVIST ... (1 Reply)
Discussion started by: saravanamr
1 Replies

8. Shell Programming and Scripting

how to insert text between lines of an existing file using perl

Hi , I need some inputs on how to open a file (file.txt) and parse the text example aaa of the file and bbb of the file and add the text zzzz once i parse (aaa and bbb) and followed by the remaining of the text as it is in the file using perl programming. Thanks in advance (3 Replies)
Discussion started by: madhul2002
3 Replies

9. 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

10. UNIX for Advanced & Expert Users

Shell script to append a time for the existing error log file

Hi Guys, the requirement is like this, i am having a error log file in this format, 4594.493: parallel nursery GC 2594592K->2544691K (2969600K), 30.848 ms 4605.958: parallel nursery GC 2634887K->2584986K (2969600K), 38.900 ms 4619.079: parallel nursery GC 2822555K->2774812K... (12 Replies)
Discussion started by: gsprasanna
12 Replies
Login or Register to Ask a Question