LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script that appends a file to start of other file (https://www.linuxquestions.org/questions/programming-9/script-that-appends-a-file-to-start-of-other-file-24459/)

amp2000 06-27-2002 02:19 PM

Script that appends a file to start of other file
 
Hi, I have about 1000 html files in a directory & need to append 4 lines of PHP code to the top of each of these files, it's for a PHPnuke site in case your wondering.
I also have to append 3 lines of PHP code to the end of the html files but I know I can do that with
'cat my-php-code >> *.html' or am I wrong on that??
The trouble is I have no idea how to append something to the beginning of a file.

A bash or perl script that does the job would be very much appreciated, or even a point in the right direction, as you can imagine it wouldnt be very nice to have to open all 997 files with VI & paste the code in.

Cheer's

togunter 06-27-2002 04:00 PM

something like this should do it:

#!/bin/sh
cp $1 $2.tmp
cat $2 >> $2.tmp
mv $2.tmp $2

########

the first argument to this script is a file containing the lines
that need to be inserted at the top, and the second is
the file into which the lines need to be inserted

--tim

jpweston 06-27-2002 04:07 PM

I'm not sure about "appending" to the beginning of a file, but you could reverse the process like so:

1. Append the three lines of code to the end of each .html file.
2. Append the each .html file to a separate file containing just the four PHP lines you need.

I'm thinking a for loop would do ok if you want to automate it.

j.

FredrikN 06-27-2002 04:34 PM

Hi.
I remember having the same problem last year so I wrote a quick program in C to fix the problem.

Maybe it can do the work for your with some pipes ( | )

download it and run it ./add2top

http://194.236.131.109/tmp/add2top.gz

gzip -d xxx to uncompress.........



The above is for appending att the top of an file



The add somethis att the end is much easier

cat file1 >> *.html

amp2000 06-27-2002 04:48 PM

I still cant do it but both of your replys have put me on the right track.
Thanks for the reply's.

Any ideas how I use all files in a directory ending in .html as a variable?
Sorry if I'm asking some basic questions here but I'm way in over my head here, I try to declare $1 as a variable containing all .html files in my current directory like so:
$1=*.html
& as you can see it dosent work.
How do I declare all the .html files as a variable so I can manipulate all the files at once?

Thanks in advance everyone.

Cheer's

amp2000 06-27-2002 04:49 PM

FredrickN, I'll go check that out now, cheer's

FredrikN 06-27-2002 05:30 PM

You can use this script if you want to add something att the end of your files

#!/bin/bash

for FILE in `ls *.html`; ## in sub dirs -> for FILE in `find . -name '*.html'`; ##
do
echo 'this is the php code you want to add' >> $FILE
done


Enjoy ...

amp2000 06-27-2002 06:19 PM

Quote:

#!/bin/bash

for FILE in `ls *.html`; ## in sub dirs -> for FILE in `find . -name '*.html'`; ##
do
echo 'this is the php code you want to add' >> $FILE
done
That's just what i needed:D

Thanks to everyone else aswell, all helpful answer's;)

Cheer's

amp2000 06-30-2002 09:56 AM

For future reference everyone, this also works, test is the name of the file containing the text you want to add to the top of all html files in the current directory, it work's!

find . -iname '*.html' -exec ex --cmd "r test" --cmd "wq" {} ';'


All times are GMT -5. The time now is 08:46 AM.