Hello Members,
I have to create a script to parse a text file in the following format:
Increment By:1
Max Value:999999
Related Table: Dummy_table
Related Table Column: dummy_id
Sequence Name: dummy_table_1SQ
Start With:1
and create an xml file from the above text file using KSH script as given below:
<?xml version="1.0"?>
<Sequence SeqName="dummy_table_1SQ" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SeqArea>appl/misc</SeqArea>
<SeqData>
<IncrementValue>1</IncrementValue>
<MaximumValue>999999</MaximumValue>
<StartValue>1</StartValue>
</SeqData>
<SeqDocs>
<SeqDesc>some description.</SeqDesc>
</SeqDocs>
<UseColumn TabName="DUMMY_TABLE" ColName="DUMMY_ID"/>
</Sequence>
Please let me know if you have any questions.
Thank you!
You can do something like that :
nawk -F': *' '
$1=="Increment By" { Inc = $2; next }
$1=="Max Value" { Max = $2; next }
$1=="Related Table" { Tbl = $2; next }
$1=="Related Table Column" { Col = $2; next }
$1=="Sequence Name" { Seq = $2; next }
$1=="Start With" { Sta = $2; next }
END {
fmt = "<?xml version=\"1.0\"?>\n";
fmt = fmt "\t<Sequence SeqName=\"%s\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
fmt = fmt "\t\t<SeqArea>appl/misc</SeqArea>\n";
fmt = fmt "\t\t<SeqData>\n";
fmt = fmt "\t\t\t<IncrementValue>%s</IncrementValue>\n";
fmt = fmt "\t\t\t<MaximumValue>%s</MaximumValue>\n";
fmt = fmt "\t\t\t<StartValue>%s</StartValue>\n";
fmt = fmt "\t\t</SeqData>\n";
fmt = fmt "\t\t<SeqDocs>\n";
fmt = fmt "\t\t\t<SeqDesc>some description.</SeqDesc>\n"
fmt = fmt "\t\t</SeqDocs>\n";
fmt = fmt "\t\t<UseColumn TabName=\"%s\" ColName=\"%s\"/>\n";
fmt = fmt "\t</Sequence>\n";
printf fmt, Seq, Inc, Max, Sta, toupper(Tbl), toupper(Col);
}
' inputfile
Inputfile:
#Begin
Increment By:1
Max Value:999999
Related Table: Dummy_table
Related Table Column: dummy_id
Sequence Name: dummy_table_1SQ
Start With:1
#End
Output:
<?xml version="1.0"?>
<Sequence SeqName="dummy_table_1SQ" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SeqArea>appl/misc</SeqArea>
<SeqData>
<IncrementValue>1</IncrementValue>
<MaximumValue>999999</MaximumValue>
<StartValue>1</StartValue>
</SeqData>
<SeqDocs>
<SeqDesc>some description.</SeqDesc>
</SeqDocs>
<UseColumn TabName="DUMMY_TABLE" ColName="DUMMY_ID"/>
</Sequence>
Jean-Pierre.
1 Like
Here is solely Korn shell solution:
trim()
{
trimmed=$1
trimmed=${trimmed%% }
trimmed=${trimmed## }
echo $trimmed
}
IFS=:
while read label value
do
case $label in
'Max Value') MAXVALUE=$(trim $value) ;;
'Start With') STARTWITH=$(trim $value) ;;
'Increment By') INCBY=$(trim $value) ;;
'Sequence Name') SEQNAME=$(trim $value) ;;
'Related Table') typeset -u RELTABLE=$(trim $value) ;;
'Related Table Column') typeset -u RELTABLECOL=$(trim $value) ;;
esac
done < infile
echo '<?xml version="1.0"?>'
echo '<Sequence SeqName="'$SEQNAME'" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
echo ' <SeqArea>appl/misc</SeqArea>'
echo ' <SeqData>'
echo ' <IncrementValue>'$INCBY'</IncrementValue>'
echo ' <MaximumValue>'$MAXVALUE'</MaximumValue>'
echo ' <StartValue>'$STARTWITH'</StartValue>'
echo ' </SeqData>'
echo ' <SeqDocs>'
echo ' <SeqDesc>some description.</SeqDesc>'
echo ' </SeqDocs>'
echo ' <UseColumn TabName="'$RELTABLE'" ColName="'$RELTABLECOL'"/>'
echo '</Sequence>'
exit 0
1 Like
Thank you! I am grateful to Jean Pierre and fpmurphy
---------- Post updated at 12:11 PM ---------- Previous update was at 11:32 AM ----------
I tried the awk script provided by Jean.
I have a question related to the same.
In the example I have provided 'appl/misc' is constant and is used as it is.
<SeqArea>appl/misc</SeqArea>
What if the SeqArea is not defined and I have to search for an existing file which has this information and then substitute it within SeqArea tags?
What I am thinking is, I will do a grep at a particular location and search for the SeqArea tag in other existing file of the same type and save it in another variable say $area.
Now my question is, how can I define this variable $area within the awk script and substitute it in place of existing appl/misc within the SeqArea tags?
Suppose the extra parameter in your file is call "Sequence Area", here is how to modify the nawk script to do what you want:
nawk -F': *' -v SeqArea='newarea' '
$1=="Increment By" { Inc = $2; next }
$1=="Max Value" { Max = $2; next }
$1=="Related Table" { Tbl = $2; next }
$1=="Related Table Column" { Col = $2; next }
$1=="Sequence Name" { Seq = $2; next }
$1=="Start With" { Sta = $2; next }
$1=="Sequence Area" { SeqArea = $2; next }
END {
fmt = "<?xml version=\"1.0\"?>\n";
fmt = fmt "\t<Sequence SeqName=\"%s\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
fmt = fmt "\t\t<SeqArea>%s</SeqArea>\n";
fmt = fmt "\t\t<SeqData>\n";
fmt = fmt "\t\t\t<IncrementValue>%s</IncrementValue>\n";
fmt = fmt "\t\t\t<MaximumValue>%s</MaximumValue>\n";
fmt = fmt "\t\t\t<StartValue>%s</StartValue>\n";
fmt = fmt "\t\t</SeqData>\n";
fmt = fmt "\t\t<SeqDocs>\n";
fmt = fmt "\t\t\t<SeqDesc>some description.</SeqDesc>\n"
fmt = fmt "\t\t</SeqDocs>\n";
fmt = fmt "\t\t<UseColumn TabName=\"%s\" ColName=\"%s\"/>\n";
fmt = fmt "\t</Sequence>\n";
printf fmt, Seq, SeqArea, Inc, Max, Sta, toupper(Tbl), toupper(Col);
}
' infile