automate editing of a template


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting automate editing of a template
# 1  
Old 07-05-2012
automate editing of a template

Given two files:

File1 - source data
abc
def
ghi
File 2 - a template to be edited with data from File 1
This is a line with a @tag@ of some item A
This is a line with a @tag@ of some item B
This is a line with a @tag@ of some item C
What I want to do is substitute @tag@ with a value from File 1, so the resulting file 2 wold be
This is a line with a abc of some item A
This is a line with a dev of some item B
This is a line with a ghi of some item C
I'm comfortable with simple to moderate shell scripting. Have lots of scripts that use sed to make global change of simple values from a single, known input value, but this one has me stumped. In the above, I'm not wedded to the idea of a single @tag@, - have no problem with using @tag1@, @tag2@, etc, if it simplifies things.

Thanks for any pointers.
# 2  
Old 07-05-2012
Code:
awk 'FILENAME=="file1" {arr[FNR]=$0; next}
       FILENAME=="file2" {sub("@tag@", arr[FNR], $0);  print}' file1  file2 > newfile

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 07-05-2012
Perfect! Thanks for the quick reply. Now I'll be spending the rest of the afternoon parsing through it to understand how it works. Always trying to expand my understanding.
# 4  
Old 07-05-2012
It's fairly simple once you know what the special variables are. FILENAME is a special variable meaning the current file being read by awk. FNR is a special variable meaning 'line number', relative to the current file. (NR would be the total number of lines from all files processed so far.) $0 is the current line.

arr is just an array with strings being put in it for each line. Once the second file gets read, it substitutes strings from that array into the lines being read, then prints them back out.

Code:
awk '
# For each line, test whether the current FILENAME is file1.
# When it is, store arr[line number]=line.
# Then skip to the next line.
FILENAME=="file1" {arr[FNR]=$0; next}

# For each line, test whether the current FILENAME is file2.
# When it is, substitute arr[line number] for @tag@, then print.
FILENAME=="file2" {sub("@tag@", arr[FNR], $0);  print}' file1 file2 > file3

Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert vi editing to text editing

Dear Guru's I'm using Putty and want to edit a file. I know we generally use vi editor to do it. As I'm not good in using vi editor, I want to convert the vi into something like text pad. Is there any option in Putty to do the same ? Thanks for your response. Srini (6 Replies)
Discussion started by: thummi9090
6 Replies

2. Programming

Calling template at once

Hello Again, I am just wanted to know if we can call the Template using "require_once" at PHP? Any views around happy to discuss. Thanks in Advance (2 Replies)
Discussion started by: AimyThomas
2 Replies

3. Programming

C++ template error

I get some compiling errors about template instantiation :wall: , but I can't find where the syntax errors happens. Can some help me? template<typename Type> class SingleList; template<typename Type> class SingleListNode{ private: friend class SingleList<Type>; SingleListNode() :... (1 Reply)
Discussion started by: 915086731
1 Replies

4. Shell Programming and Scripting

Help with template like solution

hi experts, i'm trying to do this: file1 is a template. might have kinds of 'funny' characters. sample: <body> <form> <p><input type="text" name="abc"/></p> &nbsp; <p><my_content></p> </form> </body> file2 is a file that contains lots of text. this might be very big. might have... (2 Replies)
Discussion started by: xjohnu
2 Replies

5. Programming

Template problem ...

Hi all, Need your help. I am doing a simple template program , getting some error ... here is the code #include <iostream> #include <stdio.h> #include <stdlib.h> #include<iostream> #include<string> #include <sstream> using namespace std; class Base_class { public: Base_class(){... (1 Reply)
Discussion started by: amartya_sock
1 Replies

6. Programming

About template constraints

Hi, i have class template, 1)can i override the copy constructor 2)can we have virtual function in class template if not plz tel why? I tried , compile error comes for me... Thanks Sarwan (0 Replies)
Discussion started by: sarwan
0 Replies
Login or Register to Ask a Question