Sponsored Content
Top Forums Shell Programming and Scripting Using SED to generate new file from template Post 302337971 by ppucci on Monday 27th of July 2009 02:10:21 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 08:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy