Using SED to generate new file from template


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using SED to generate new file from template
# 1  
Old 07-25-2009
Using SED to generate new file from template

Hi there!

I am using a BASH script to read a CSV file (containing variable values)using while read, and for every record I want SED to get a template from a file, and using the variables read from the CSV, write a new file.

Code:
#!/bin/bash
current_ifs=$IFS ; #backup original IFS, need "," for thw CSV file
IFS=,
while read VAR1 VAR2 VAR3 FILENAME; do
 sed -n -e 's/VAR1/$VAR1/g'\
 -e 's/VAR2/$VAR2/g'\
 -e 's/VAR3/$VAR3/g'\
 TEMPLATE w $FILENAME 
done < $CSV
IFS=$current_ifs; #redefines original IFS
exit

But it looks like I am missing something. TEMPLATE is on the same folder, new file can go to any folder (would be nice to set it)

Help anyone?
# 2  
Old 07-26-2009
Just the code is not enough.
Give a listing of the contents of all the files involved.
What is this TEMPLATE thing?
What is the "w" in TEMPLATE w $FILENAME?
What should the output look like?
In this forum,if you dont get a response in 1 or 2 hours, then most probably, your question is not clear.
# 3  
Old 07-26-2009
I've read somewhere about this "w" option on writing the output to a different file than input, but I guess I read it wrong or not sure how to use it...

Anyway, I went back to redirect using > and it worked!

Just for the record... the template files are templates with placeholder for Cisco router config files, like:

Code:
!configlet for HNAME (IP_ADD)
conf t
interface INT
 ip address IP_ADD mask MASK
end

And the source CSV file contains the hostname, interface number, IP addresses and subnet masks for each of the 1000+ devices I needed to create a configuration file... Smilie CSV also had the device type for each device (using the same types as TEMPLATE) and a FILENAM for each device so the script would write a different file for each device.

As I had many different templates, I also improved the code to save them on different folders according to the type.

I'll post latter the final script Smilie

Thank you for your reply!
# 4  
Old 07-26-2009
Yes, you are right, with the "w" you can write the output to a file.
but the syntax should be as follows:
Code:
w TEMPLATE $FILENAME

Where "TEMPLATE" will be the outputfile.

Did yo realize that you have used the FILENAME variable at both, the "read" as well as the input file name?
# 5  
Old 07-26-2009
@ ppucci
Why you don't post a sample input and a sample of required output.
# 6  
Old 07-27-2009
Quote:
Originally Posted by edidataguy
Yes, you are right, with the "w" you can write the output to a file.
but the syntax should be as follows:
Code:
w TEMPLATE $FILENAME

Where "TEMPLATE" will be the outputfile.

Did yo realize that you have used the FILENAME variable at both, the "read" as well as the input file name?
Well... guess I'll try "w" next time now I know how it works Smilie... does it have any difference to > after all?

As for the FILENAME var... I actually read it from the CSV archive (it is one of the columns) and then I apply it as the name of the output file (that's what I did on my finished script... but right, at my first example it was (wrongly) on the input file). That "while read" now reads:

Code:
while read VAR1 VAR2 VAR3 FILENAME; do
 sed -n -e "s/VAR1/$VAR1/g"\
 -e "s/VAR2/$VAR2/g"\
 -e "s/VAR3/$VAR3/g"\
 TEMPLATE > $FILENAME 
done < $CSV

Again, this is just an example on who it looks now... I'll post the actual script later Smilie

---------- Post updated 07-27-09 at 01:10 PM ---------- Previous update was 07-26-09 at 02:08 PM ----------

One last thing...

one of the columns on the csv file has values with spaces on it (i.e: interface name) but sed brings that around quotes (i.e: "interface name") and in some cases, it brings in quotes and adds a string to the end (i.e: "interface name"ESS).

What gives? Is there a way to tell sed not to do that?

By the way.... as promissed, here is the script that is working now:

Code:
#!/bin/bash
current_ifs=$IFS; #backup original IFS
IFS=,
echo "reading $1"
# while and sed are broken down for readability
while read REGION COUNTRY SITE_ID BU MGMT_SUBNET GATEWAY_MGMT\ 
 IP_MGMT NEW_MASK BLDG_NAME HNAME MODEL SERIAL SOC IOS\
 CUR_MGMT_IP CUR_MGMT_MASK CUR_MGMT_INT DEVTYPE TRUNK_TYPE\
 ROU_1_IP ROU_2_IP DIST_SW1_IP_OR_NH DIST_SW2_IP\
 ROUTING_PROTO_TYPE ROUTING_PROC OSPF_AREA HSRP AB\
 STATUS_VLAN_997 OBS UnID TRUNK_DATA FILENAME; do
 sed -e "s|HNAME|$HNAME|g"\
 -e "s|REGION|$REGION|g"\
 -e "s|SITE_ID|$SITE_ID|g"\
 -e "s|CUR_MGMT_IP|$CUR_MGMT_IP|g"\
 -e "s|CUR_MGMT_INT|$CUR_MGMT_INT|g"\
 -e "s|IP_MGMT|$IP_MGMT|g"\
 -e "s|NEW_MASK|$NEW_MASK|g"\
 -e "s|GATEWAY_MGMT|$GATEWAY_MGMT|g"\
 -e "s|MGMT_SUBNET|$MGMT_SUBNET|g"\
 -e "s|CUR_MGMT_IP|$CUR_MGMT_IP|g"\
 -e "s|CUR_MGMT_MASK|$CUR_MGMT_MASK|g"\
 -e "s|CUR_MGMT_INT|$CUR_MGMT_INT|g"\
 -e "s|ROU_1_IP|$ROU_1_IP|g"\
 -e "s|ROU_2_IP|$ROU_2_IP|g"\
 -e "s|DIST_SW1_IP_OR_NH|$DIST_SW1_IP_OR_NH|g"\
 -e "s|DIST_SW2_IP|$DIST_SW2_IP|g"\
 -e "s|ROUTING_PROTO_TYPE|$ROUTING_PROTO_TYPE|g"\
 -e "s|ROUTING_PROC|$ROUTING_PROC|g"\
 -e "s|OSPF_AREA|$OSPF_AREA|g"\
 -e "s|TRUNK_DATA|$TRUNK_DATA|g"\
 -e "s|HSRP|$HSRP|g"\
 $UnID > "./CONFIGS/$UnID/$FILENAME"
done < $1
IFS=$current_ifs; #redefines original IFS
exit


Last edited by ppucci; 07-27-2009 at 09:20 PM..
# 7  
Old 07-28-2009
Double check your code.
Why are these lines repeated in your code? Intentionally done?
Code:
-e "s|CUR_MGMT_INT|$CUR_MGMT_INT|g"\
 -e "s|CUR_MGMT_INT|$CUR_MGMT_INT|g"\
 -e "s|CUR_MGMT_IP|$CUR_MGMT_IP|g"\
 -e "s|CUR_MGMT_IP|$CUR_MGMT_IP|g"\

I would sugest changing all the lines as follows:
Code:
; "s|CUR_MGMT_INT|$CUR_MGMT_INT|"\
; "s|CUR_MGMT_INT|$CUR_MGMT_INT|"\

Why do you need the "-e" and the "/g" options.

Regarding the " " issue post the input and the output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Generate files and use csv data to replace multiple variables in a template

I have a source csv file consists of first field as variable name, and the rest are site-specific information (converted from excel file, where site -specific values in columns). I am trying to create a file for every site using a template and replace the multiple variables with values from the... (3 Replies)
Discussion started by: apalex
3 Replies

2. Shell Programming and Scripting

Merge strings from a file into a template

I am preparing a morphological grammar of Marathi to be placed in open-source. I have two files. The first file called Adverbs contains a whole list of words, one word per line A sample is given below: आधी इतक इतपत उलट एवढ ऐवजी कड कडनं कडल कडील कडून कडे करता करिता खाल (2 Replies)
Discussion started by: gimley
2 Replies

3. Shell Programming and Scripting

Inputing info from a CSV file and filling a template file

Hi, I have a .csv file that contains a variety of fields for 60 clients: USERNAME, PASSWORD, and COMMENTS. I have a template file which contains a great deal of data for each client and has the fields USERNAME, PASSWORD, and COMMENTS that has to be filled with the values of USERNAME,... (1 Reply)
Discussion started by: mojoman
1 Replies

4. Linux

Search a template file and replace with input

Hi I have a CommonTemplateStop.template file . Inside the file i need to replace the variables DepName and CompInsName with the values(Trade and TradeIns) specified in the script. I have written the below .sh script in linux server which will read the .template file and has to replace the 2... (8 Replies)
Discussion started by: samrat dutta
8 Replies

5. UNIX for Dummies Questions & Answers

XML File Generation - Template Help

Hi, I have hit a bit of a brick wall.:confused: need the following code edited: echo "<?xml version=\"1.0\"?><dailyBalance_ROWSET>" > ${DataDir}/${extract_script}${ApplicationDate}.${Suffix} RunSQL ${extract_script} ${ActionFlag} echo "</dailyBalance_ROWSET>" >>... (2 Replies)
Discussion started by: Xergxes7
2 Replies

6. Shell Programming and Scripting

Creating a larger .xml file from a template(sample file)

Dear All, I have a template xml file like below. ....Some---Header....... <SignalPreference> ... <SignalName>STRING</SignalName> ... </SignalPreference> ......Some formatting text....... <SignalPreference> ......... ... (3 Replies)
Discussion started by: ks_reddy
3 Replies

7. Shell Programming and Scripting

filling in strings in a template file using awk

Hi all, I have a template form to fill in for quite a number of files and I want to automate the filling-in process. the concept seemed to be simple but i cant get it work. the template form is a text file containing the information below: File Name: Date Created: Contents: I need to... (4 Replies)
Discussion started by: ida1215
4 Replies

8. Shell Programming and Scripting

Reading columns, making a new file using another as template

Hi fellas, I have two files such as: File 1 interacao,AspAsp,AspCys,CysAsp,CysCys,classe File 2 interacao,AspAsp,CysAsp,AspCys,CysCys,classe beta_alfa, DA, CA, DD, CD,ppi Thus, I want to make a File 3 using the File 1 as model: e.g. File 3... (2 Replies)
Discussion started by: valente
2 Replies

9. Shell Programming and Scripting

sed script to generate hyperlinks refuses to work

Hi All, I'm new to the forum and not a programmer, but I'm writing a bash script to preprocess definitions of technical terms by inserting hyperlinks pointing to other pages in the glossary before the pages are posted to our server, using a standard naming convention for the pages. The... (3 Replies)
Discussion started by: markfgilliland
3 Replies

10. Shell Programming and Scripting

configuration and template file

Hi, I have a configuration file(which has values) and a template file(where the values are dummied) Configuration file (a.txt) -------------------- var1=1521 var2=172.10.10.10 var3=emp . . . var15=hhhhhhh Template file (b.txt) -------------------- The host name is $var2. The... (5 Replies)
Discussion started by: ammu
5 Replies
Login or Register to Ask a Question