script to insert variable file into another, bit like C's #include


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to insert variable file into another, bit like C's #include
# 1  
Old 08-13-2010
script to insert variable file into another, bit like C's #include

Hi, I want to insert one file into another like this:

file 1:
1
2
3
<!-- #include file="file2" -->
4
5
<!-- #include file="file3" -->
6

with the complete output going to another file. Have seen some solutions to similar problems using sed, but this problem is different because it is unknown at the command line which files will need to be included.

Can this be done with sed or is there something better?

Cheers

Last edited by cvq; 08-13-2010 at 11:13 AM.. Reason: makes more sense
# 2  
Old 08-13-2010
You can "source" the files in your script like:
Code:
1
2
3
. ./file2
4
5
. ./file3
6

But I'm not sure if this is what you mean.
# 3  
Old 08-13-2010
If the goal is to parse one "template" file into an output file, I've occasionally used PHP for that. The way it restricts itself to running code only inside PHP tags is very helpful, and you can make it as simple or as complicated as you want.

Code:
a
b
c
d
e
f
<?php readfile("filename"); ?>
g
h
i
j
k
l

Code:
php < template > output

# 4  
Old 08-13-2010
Thank you, yes I am trying to do what Corona688 describes.

I was hoping to keep the <!-- #include file="file2" --> syntax if possible, because I am writing a website using Server Side Includes and this is the syntax for that, but I'm developing the site on a local machine (which won't do SSI) so want to mimic the effect with a script.

If I can't keep the SSI syntax I will try the php thing. Would then need to replace a large number of include lines before uploading to the server... is that any easier than the original problem? Hmmm.
# 5  
Old 08-14-2010
Why not just install a webserver on your machine? Then you'll be able to use the language you want where you need it.

I don't think server side includes are that great a thing to base a site on though. My website was written in them, but I had to abandon them due to finicky incompatibilities and configuration changes every time I updated apache even slightly. Now that I've written it in PHP it's stayed stable.
# 6  
Old 08-14-2010
Well I've written a little C++ program to do the includes - bit OTT but I understand that better than most scripts.

Did that before seeing your last post Corona688 - bit of a worry you found SSI so inconsistent. Will have to have a think about that.

Thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash: Insert in a variable a file

hi all i have a problem in the bash shell. i'd like insert in a variable a file for example : i have a file datafine.log in this file there is : 17/JUN/2019 i want to insert the value of datafine.log in a variable. Regards Frncesco edit by bakunin: please use CODE-tags for your data... (2 Replies)
Discussion started by: Francesco_IT
2 Replies

2. Shell Programming and Scripting

Insert value of env variable in xml file

Hello, I have the following variables set in my env echo $MY_XSD_FILE /home/jak/sample.xsd echo $MY_INTERVAL_VALUE 4 I want to insert them between the xml tags in my xml file cat sample.xml ::::::::::::::: ::::::::::::::: <property name="FILE"></property> :::::::::::::::::::::::... (2 Replies)
Discussion started by: jakSun8
2 Replies

3. Shell Programming and Scripting

Insert a variable to a text file after fixed number of lines

Hi, I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines.. Please help me on this.. Thanks in Advance, Amrutha (3 Replies)
Discussion started by: amr89
3 Replies

4. Shell Programming and Scripting

How to insert variable date (monthly) from SQL script

Hi Guys, Can someone please help me on adding/inserting a variable to an sql scipt? Basically I want to generate data on monthly (i.e. July 01, 2011 to July 31, 2011) basis. As shown below.. set head off; set linesize 300; set pagesize 200; spool /opt/oracle/temp/output.txt select... (1 Reply)
Discussion started by: pinpe
1 Replies

5. Shell Programming and Scripting

script to include filename with variations at the beginning of the file

Hi there, I have a bunch of files that I want to modify. As a beginner, but nevertheless enthusiast, in the use of the shell I want to create a script enabling me to modify those files quickly. I had only some partial success with various peaces of scripts but I would like to create one script... (1 Reply)
Discussion started by: iamzesh
1 Replies

6. Shell Programming and Scripting

Insert text into file depending on variable

Hey guys , i have a variable with the contents ... NUMBER=4 and a test file with the contents 1248 1213 1214 1278 1200 3045 3444 2130 I want to execute a script that will produce the following output ( based on NUMBER=4) to be ... create 1248 (1 Reply)
Discussion started by: theshams
1 Replies

7. Shell Programming and Scripting

insert line into file using variable

Hi all, I am trying to insert couple of lines before first occurance of line3 which occuring after line 5. So I identified the line 5 line number in the file. Also redirected the all line3 line number to out.txt. Now I have problem in inserting the line using the variable $rep. Please help me... (2 Replies)
Discussion started by: aimmanuel
2 Replies

8. Shell Programming and Scripting

insert a variable in the last column of a file

i want to insert a variable in the last column of a file, the columns are separated by "|". I want to insert the variable in everyline of the file . (7 Replies)
Discussion started by: dineshr85
7 Replies

9. Shell Programming and Scripting

how to include header file in b-sh script

I'm new in this. In C/C++, you define variables in one header file & then simply include it in any files where those variables are referenced. I tried to include a header named DefineVars- which initializes all the globals- but when I add the line "DefineGlobals" at the begining of another... (2 Replies)
Discussion started by: bluemoon1
2 Replies

10. Programming

Writing both 8-bit and 16-bit data to a file

I'm writing both 8-bit and wide 16-bit data to the screen and an output file. I have no problems with writing out to the screen - for example: cout<<8-bit data; wcout<<16-bit data; Similarly, I have used ofstream for 8-bit and wofstream for 16-bit, for example: ofstream out; wofstream wout;... (1 Reply)
Discussion started by: Breen
1 Replies
Login or Register to Ask a Question