create an xml file using a shell script

2019-08-25 20:19发布

问题:

I have a table with two columns

column_1 column_1
12345     12345
73255     73255
71377     71377 

Now i want to create an xml like

<header>
<value>12345</value>
<value>73255</value>
<value>71377</value>
<footer>

basically i need to use a select query and put any one of the fields into the values of xml.

could you please suggest how could this be done in an easiest way? much appreciate your help.

回答1:

imagine you have selected from the database and stored those columns in a file called "file"

#!/bin/bash
awk 'BEGIN{ print "<header>"}
NR>1{  print "<value>"$1"</value>" }
END{ print "<footer>"}' file

on the command line

# ./shell.sh
<header>
<value>12345</value>
<value>73255</value>
<value>71377</value>
<footer>