Using SED to generate new file from template


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using SED to generate new file from template
# 8  
Quote:
Originally Posted by edidataguy
I would sugest changing all the lines as follows:
Code:
; "s|CUR_MGMT_INT|$CUR_MGMT_INT|"\
; "s|CUR_MGMT_INT|$CUR_MGMT_INT|"\

Hey, thank you for that... I did not see it was duplicated... it did not keep the script from running, it just addded unecessary steps to it.. Smilie

Quote:
Originally Posted by edidataguy
Why do you need the "-e" and the "/g" options.
Well, I am new to sed, but from what I read -e is to inform that the following is still part of the script (instead of going sed "1..."; sed "2..."; sed "3..." you use sed -e "1..." -e "2..." -e "2..."). As for g, it is to replace all occurrences of searched text, isn't it? Well they may appear more than once on the template files...

Quote:
Originally Posted by edidataguy
Regarding the " " issue post the input and the output.
Here is one record from the source file (all sensitive data was replaced, line break for readability):

Code:
persio@dynaserver:~/CUS$ more test
,"United States",226,,10.10.10.@,,10.10.10.@,255.255.255.128,Springfield,coresw1.cutomer.com,
WS-C3750G-12S-E,SERIALNO,SWIA8,12.2(35)SE5,10.10.10.@,255.255.255.192,Vlan997,"Switch Co
re L3",1,10.10.10.10,,,,2,ospf 12345,area 0,hsrp,,,,SWL3-2,#N/A,226-10_10_10_@-SWL3-2.txt
persio@dynaserver:~/CUS$

This record will use the following template according to its UnID (in red):

Code:
persio@dynaserver:~/CUS$ more SWL3-2
config t
!
vlan 996
desc Management VLAN
!
interface vlan 996
ip address IP_MGMT NEW_MASK
!
router ROUTING_PROCESS
network IP_MGMT 0.0.0.0 OSPF_AREA
!
! Trunk Configuration
TRUNK_DATA
! End trunk configuration
end
persio@dynaserver:~/CUS$

And when I run the script, this is the output file:

Code:
persio@dynaserver:~/CUS$ ./generate_config.sh test
reading test
persio@dynaserver:~/CUS$ more CONFIGS/SWL3-2/226-10_10_10_@-SWL3-2.txt
config t
!
vlan 996
 desc Management VLAN
!
interface vlan 996
 ip address 10.10.10.@ 255.255.255.128
!
router "ospf 10226"ESS
 network 10.10.10.@ 0.0.0.0 "area 0"
!
! Trunk Configuration
interface FastEthernet0/0
 switchport trunk allowed vlan add 996
! End trunk configuration
end
persio@dynaserver:~/CUS$

Details in blue are placeholder (on template) and replaced data (on output). Deatils in orange are unwanted characters that sed is including to the output file. I wanted to understand (1) why is it happening and (2) what can I do to prevent that?
# 9  
Well too many things in one go.
Lets split them.
Ragarding:
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"\

You need not call sed again and again.
As an example, the code can be as follows:
Code:
sed \
"s|HNAME|$HNAME|;   \
 s|REGION|$REGION|;  \
 s|SITE_ID|$SITE_ID|; \
.............."

In your case you don't need the "/G". Without it, the command will run faster.
# 10  
Quote:
Originally Posted by edidataguy
Well too many things in one go.
Lets split them.
Ragarding:
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"\

You need not call sed again and again.
As an example, the code can be as follows:
Code:
sed \
"s|HNAME|$HNAME|;   \
 s|REGION|$REGION|;  \
 s|SITE_ID|$SITE_ID|; \
.............."

In your case you don't need the "/G". Without it, the command will run faster.
Ok, I'll give it a try without "-e" and "/g"... and for the duplicate replacements, already scraped them Smilie

---------- Post updated at 01:51 PM ---------- Previous update was at 01:43 PM ----------

tried... worked just fine... on this particular case, speed was not really an issue as the script ran for about 10 seconds or less (for a 950 records source file).

Will keep that in mind on my next scripts!
# 11  
Regarding " " and the ESS thing, all that I can think of is there is some junk char somewhere.
The first thing I will do is search for \r\n and replace with \n for both, the input as well as the code and run them again.
Else look for some other chars which you cannot see.
Beyond this I cannot help you, sorry.
If you do fix it, please do get back.
# 12  
Quote:
Originally Posted by edidataguy
Regarding " " and the ESS thing, all that I can think of is there is some junk char somewhere.
The first thing I will do is search for \r\n and replace with \n for both, the input as well as the code and run them again.
Else look for some other chars which you cannot see.
Beyond this I cannot help you, sorry.
If you do fix it, please do get back.
I fixed the quotes... I actually rebuilt the csv file from the Excel original. No more quotes now. However, I still get the ESS. I was careful to open the csv file at Notepad++ (Windows application) that allows me to convert the file to UNIX format. After that, I opened the file on Ubuntu with gnumeric, all good, no hidden chars.

Funny thing is that I only get it on one specific field (ROUTING_PROC), which I usually have ospf 1234 or eigrp 1234 as a value (numbers may actually vary). It keeps bringing ospf 1234ESS BUT it will not do it to eigrp 1234 values. That is rather strange. Did you have the chance to try it using the script and the sample input record?

Thank you again for your attention!
# 13  
post both the input records.
"ospf 1234" and "eigrp 1234"
Have a feeling it is messed up because of the field next/before to it (#?).
Just a hunch.

Another way is change you code:
router ROUTING_PROCESS
to
router xxx ROUTING_PROCESS yyy
see what happens.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #158
Difficulty: Easy
The migration of the ARPANET to TCP/IP was officially completed on January 1, 1983, when the new protocols were permanently activated.
True or False?

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

Featured Tech Videos