Help with template like solution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with template like solution
# 1  
Old 04-28-2010
Help with template like solution

hi experts,

i'm trying to do this:

file1 is a template. might have kinds of 'funny' characters.
sample:
Code:
<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 'funny' characters as well.
sample:
Code:
<tr><td align="center">1.1</td><td>1.2</td></tr>
... many many lines ...
<tr><td align="center">2000.1</td><td>2000.2</td></tr>

my question is, how do i read file1, replace <my_content> with the content of file2, and store/print out the result?

i've tried to use sed, i firstly escape the content of file2, these characters: \ &
then i use sed substitute. but when file2 gets very big, sed would display error that the parameter is too long.

any suggestions on how to do this? i do not need the regex feature. just search that <my_content> and replace it with the content from file2.

thanks for the help.
# 2  
Old 04-28-2010
Hope you can modify your template like (so that "<my_content>" has its own line).
Code:
<body>
<form>
<p><input type="text" name="abc"/></p>
&nbsp;
<p>
<my_content>
</p>
</form>
</body>

Then this works (takes everything before "<my_content>" , the file2 and then everything after "<my_content>" )
Code:
#!/bin/bash
{   sed -n '1,/\<my_content\>/p' file1 | sed '$d'
    cat file2
    sed -n '/\<my_content\>/,//p' file1 | sed '1d'
} > file3

If "<my_content>" is an html comment, you can remove everything after the pipes in the script, it will leave the comment in the output file as markers to see where you've put your data.
# 3  
Old 04-28-2010
An AWK solution :
Code:
awk '
   NR==FNR { string = string $0 "\n" ; next}
   { gsub(/<my_content>/, string) ; print }
' file2 file1

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

vi calling template

Hello. I want to copy temp files when I make a new file by vi. For example, 09:32:52 ~/ $ mkdir test 09:33:03 ~/ $ cd test/ 09:33:09 ~/test/ $ ls 09:33:16 ~/test/ $ vi test.cpp 09:34:37 ~/test/ $ cat test.cpp #include <iostream> int main() { } 09:34:48 ~/test/ $ vi test.bash 09:35:19... (1 Reply)
Discussion started by: Euler04
1 Replies

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