Insert values into template


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert values into template
# 1  
Old 06-06-2016
Insert values into template

I have 2 files.

Code:
Template file:
[TYPE]SELECT[/TYPE]
[BEGIN][/BEGIN]
[END][/END]
[SECTION]NAME = ""[/SECTION]
[VALUE]DEATILS[/VALUE]

Code:
Input file:
SERVER1 06/05/2016 10:00:00 06/05/2016 05:08:59
SERVER2 06/04/2016 09:50:00 06/05/2016 01:03:59
SERVER3 06/06/2016 11:26:00 06/06/2016 10:31:55

I want to generate the output file that look like this:
Code:
[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]

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.
Code:
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


Last edited by vinus; 06-06-2016 at 04:02 PM..
# 2  
Old 06-06-2016
You need to hardcode something, being the values in your input really don't really resemble the values in your output.
# 3  
Old 06-06-2016
Quote:
Originally Posted by Corona688
You need to hardcode something, being the values in your input really don't really resemble the values in your output.
Sorry, that was typo error. It is "Line".
# 4  
Old 06-06-2016
Any preferences as to the tool to be used?
# 5  
Old 06-06-2016
Quote:
Originally Posted by RudiC
Any preferences as to the tool to be used?
We can use awk or sed command.
# 6  
Old 06-06-2016
Not too sophisticated, but may pass for a zeroth approach:
Code:
awk '
NR==FNR         {T[NR] = $0
                 MX = NR   
                 next   
                }

                {X = T[2]
                 Y = T[3]
                 Z = T[4]
                 sub ("]\[", "]" $2 " " $3 "[", X)
                 sub ("]\[", "]" $4 " " $5 "[", Y)
                 sub (/""/, "\"" $1 "\"",  Z)
                 print T[1]
                 print X
                 print Y
                 print Z   
                 print T[5]
                }
' file2 file1
[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]

This User Gave Thanks to RudiC For This Post:
# 7  
Old 06-06-2016
Using my yanx.awk library I can substitute into appropriate tag names:

Code:
$ 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 .
These 2 Users Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert values into a file 0 as per the date wise

Hi The file contains 12 months of date and less than 12 months of data I want to display if date filed less than 12 months of data I want to insert a value amount 1 to amount4 0 and dates as well. 12345|Date|cntry|amount1|amount2|amount3|amoun4... (2 Replies)
Discussion started by: jagu
2 Replies

2. Shell Programming and Scripting

Insert missing values

Hi, please help with this, I need to insert missing values into a matrix for a regression analysis. I have made up an example. The first three columns are variables with levels and the next 3 are values, the 4th column missing values should be replaced by 0s, and 5th and 6th column missing... (3 Replies)
Discussion started by: ritakadm
3 Replies

3. Shell Programming and Scripting

Insert bulk values in DB table using isql

Hello, Objective is to insert bulk values in DB table using isql. Following code tried: isql -SServer_name -Ddb_name -Uuser_name -Ppassword < file.txt cat file.txt for i in `cat data_value_file.txt` do insert into tempdb..temp_table11 values ('$i') go done cat... (3 Replies)
Discussion started by: manishdivs
3 Replies

4. Shell Programming and Scripting

Insert strings/values between text

Hello Guru, I'm trying to insert a value between 2 fields (between last and second last field) But end up the script actually replacing the value in the second last field. What should i put to fix the problem? Input File: apple,mango,grape,lemonExpected output: apple,mango,grape,0,lemon awk... (5 Replies)
Discussion started by: null7
5 Replies

5. Shell Programming and Scripting

insert dummy values in a file

Hey guys, i have a file a.txt and i need to insert a dummy data into it as per the below pattern.. bash: cat a.txt 1234,34 3434,45 4545,56 3434,56 Now i need the file in the below pattern 1234,34,a0001,C_01 3434,45,a0002,C_02 4545,56,a0003,C_03 3434,56,a0004,C_04 here the count of... (3 Replies)
Discussion started by: jaituteja
3 Replies

6. Shell Programming and Scripting

Insert values

HI Guys, I have a data in a file in the below format 45783 23457 23556 54584 Now i want to convert this data into the below format reader='45783' or reader='23457' or reader='23556' or reader='54584' Please help how to convert as i am applying loop but not able to get the data... (6 Replies)
Discussion started by: jaituteja
6 Replies

7. Shell Programming and Scripting

Insert Inverted Commas Around Numeric Values

Hi, I am trying to insert Inverted Commas around all the numeric values within a comma seperated string / variable. 1111,2222,3333,4444 I would like it to be: '1111','2222','3333','4444' Note - This string could have a differing amount of numeric values each time the variable is... (4 Replies)
Discussion started by: RichZR
4 Replies

8. Shell Programming and Scripting

insert values into sqlplus database using shell script

hello all, I am new to shell scripting and to unix... so the following is my assignment.. here i am trying to insert a values into sqlplus database using shell script. the following is my shell script InsertDelete.sh #! /bin/sh echo "*********The MENU******** 1.Insert The Values... (2 Replies)
Discussion started by: pradeept
2 Replies

9. Shell Programming and Scripting

hw to insert array values sequentially in a file

Hi All :), I am very new to unix. I am requiring ur help in developing shell script for below problem. I have to replace the second field of file with values of array sequentially where first field is ValidateKeepVar <File> UT-ExtractField 1 | &LogEntry &Keep(DatatoValidate)... (3 Replies)
Discussion started by: rohiiit.sharma
3 Replies

10. UNIX for Advanced & Expert Users

insert pipe in file to separate values

hi all... i need your help, because i donīt know what to do... i have a flat file like this: B065200512312004123111010000061451 000021853 B065200512312004123111020000621907 000417802 B065200512312004123111030000005214 000005861 B065200512312004123111040000120133 000088448 and i need... (5 Replies)
Discussion started by: DebianJ
5 Replies
Login or Register to Ask a Question