LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2002, 02:19 PM   #1
amp2000
Member
 
Registered: Oct 2001
Location: Dublin, Ireland
Distribution: Mandrake 9.0 mostly!
Posts: 303

Rep: Reputation: 30
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
 
Old 06-27-2002, 04:00 PM   #2
togunter
LQ Newbie
 
Registered: Apr 2002
Location: Los Angeles
Distribution: Red Hat 7.0 & 7.2
Posts: 4

Rep: Reputation: 0
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
 
Old 06-27-2002, 04:07 PM   #3
jpweston
Member
 
Registered: Mar 2002
Location: Sacramento, CA
Distribution: Slackware 8.1; Debian 3.0
Posts: 222

Rep: Reputation: 30
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.
 
Old 06-27-2002, 04:34 PM   #4
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
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

Last edited by FredrikN; 06-27-2002 at 04:37 PM.
 
Old 06-27-2002, 04:48 PM   #5
amp2000
Member
 
Registered: Oct 2001
Location: Dublin, Ireland
Distribution: Mandrake 9.0 mostly!
Posts: 303

Original Poster
Rep: Reputation: 30
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
 
Old 06-27-2002, 04:49 PM   #6
amp2000
Member
 
Registered: Oct 2001
Location: Dublin, Ireland
Distribution: Mandrake 9.0 mostly!
Posts: 303

Original Poster
Rep: Reputation: 30
FredrickN, I'll go check that out now, cheer's
 
Old 06-27-2002, 05:30 PM   #7
FredrikN
Member
 
Registered: Nov 2001
Location: Sweden
Distribution: GNU/Linux since -97
Posts: 149

Rep: Reputation: 15
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 ...

Last edited by FredrikN; 06-27-2002 at 05:45 PM.
 
Old 06-27-2002, 06:19 PM   #8
amp2000
Member
 
Registered: Oct 2001
Location: Dublin, Ireland
Distribution: Mandrake 9.0 mostly!
Posts: 303

Original Poster
Rep: Reputation: 30
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

Thanks to everyone else aswell, all helpful answer's

Cheer's
 
Old 06-30-2002, 09:56 AM   #9
amp2000
Member
 
Registered: Oct 2001
Location: Dublin, Ireland
Distribution: Mandrake 9.0 mostly!
Posts: 303

Original Poster
Rep: Reputation: 30
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" {} ';'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Start a file eye7 Programming 3 08-09-2005 04:42 PM
How to start a Tcl/Tk script by simply invoking the script file itself ? cyu021 Programming 2 10-10-2004 11:00 AM
bash shell script appends '?' to filenames sceptreofjudah Linux - Newbie 3 03-18-2004 08:54 AM
How to play a media file/ video file/mp3 file recorded in harddisk/cd-rom arindam Linux - Newbie 2 09-05-2003 10:31 AM
Script, Reading a file, When end of file? elibm Programming 2 07-16-2001 11:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration