My code is working fine but i dont want to hardcode the template contents inside the script. I need to read the template and then generate the output file.Any help would be greatly appreciated.
for Line in `cat ${inputfile}`
do
ServerName=`echo $Line|awk '{print $1}`
STARTDATETIME=`echo $Line|awk '{print $2,$3}`
ENDDATETIME=`echo $Line|awk '{print $4,$5}`
echo "[TYPE]SELECT[/TYPE]
[BEGIN]${STARTDATETIME}[/BEGIN]
[END]${ENDDATETIME}[/END]
[SECTION]NAME= \"${ServerName}\"[/SECTION]
[VALUE]DEATILS[/VALUE]" >>outputfile.txt
done
$ cat template.sh
#!/bin/bash
if [ -z "$2" ]
then
echo "Usage: $0 datafile templatefile" >&2
exit 1
fi
while read NAME D1 T1 D2 T2 G
do
awk -v DATA="BEGIN|$D1 $T1|END|$D2 $T2|SECTION|NAME = \"$NAME\"" \
-f yanx.awk -e 'BEGIN {
RS="["; FS="]"; ORS=""; OFS="]";
N=split(DATA, DA, "|");
for(M=1; M<=N; M+=2) D[DA[M]]=DA[M+1]; }
TAG in D { $2=D[TAG] } ; { print "[" $0 }' "$2"
done < "$1"
$ ./template.sh
Usage: ./template.sh datafile templatefile
$ ./template.sh data template
[TYPE]SELECT[/TYPE]
[BEGIN]06/05/2016 10:00:00[/BEGIN]
[END]06/05/2016 05:08:59[/END]
[SECTION]NAME = "SERVER1"[/SECTION]
[VALUE]DEATILS[/VALUE]
[TYPE]SELECT[/TYPE]
[BEGIN]06/04/2016 09:50:00[/BEGIN]
[END]06/05/2016 01:03:59[/END]
[SECTION]NAME = "SERVER2"[/SECTION]
[VALUE]DEATILS[/VALUE]
[TYPE]SELECT[/TYPE]
[BEGIN]06/06/2016 11:26:00[/BEGIN]
[END]06/06/2016 10:31:55[/END]
[SECTION]NAME = "SERVER3"[/SECTION]
[VALUE]DEATILS[/VALUE]
$
The hardcoded bit, the part that transforms the variables as read into the tag names and values you want, is highlighted in red. It's formatted TAGNAME|TAGVALUE|TAGNAME|TAGVALUE .