Forum Moderators: coopster & phranque

Message Too Old, No Replies

I could really use a perl script

         

Karabaja

12:08 pm on Aug 19, 2006 (gmt 0)

10+ Year Member



I tried asking help on some linux shell scripting forums and didn't get any reply so far. So I figured perl script could probably do the job. But I got no knowledge of it and I hope someone here can help. I'd really appreciate it.
I've got like 100 txt files wich I would like to use on web page. I can use them in iframes but to display text properly that text should be wraped in preformat tags.
So I would need a script that would insert <pre> at the beginning and </pre> at the end of all txt files in a folder.
All documents start with a same word which I don't really need so alternative would be a script that replaces specified word ex "start" with <pre> and inserts </pre> at the end of each document. I certainly hope this is possible to do, it would save me a lot of trouble since these files would be updated weekly and it would be too much work to do manually.

perl_diver

8:38 pm on Aug 19, 2006 (gmt 0)

10+ Year Member



<iframe><pre>stuff here</pre></iframe>

rocknbil

8:49 pm on Aug 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Edit: Yeah that will work, I just don't like iframes, thee is something unsettling about a scroll bar in the middle of the page. :-)

You didn't get a reply because explaining how to install and set up a script is a bit more complex than a one-post deal, and has probably been asked a million times. :-) But there is an EASIER way to do this!

Does your server support Server Side Includes? Most do.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!-- Put the previous two lines ALL ON ONE LINE! -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Be sure to use a good title, it's important</title>
<meta name="Description" content="Ditto">
<meta name="keywords" content="Ditto">
</head>
<body>
<h1>The heading tag is important too</h3>
<pre>
<!-- #include file="myfile.txt" -->
</pre>
</body>
</html>

Copy and paste, change "myfile.txt" to one of your text files, and save it as test.shtml. Shtml is very important, it tells the server to look for include directives in the file. Upload it to the same directory as your text files.

The magic line is the include line. If you put your text files in a folder and not the same directory as the shtml files, use virtual instead:

<!-- #include virtual="/textfiles/myfile.txt" -->

If you try this and get "an error has ocurred while processing this directive" includes will work on your server, but you're doing something wrong - the case is wrong (Myfile.txt) or the file is not present.

If this doesn't work I have sent you a sticky mail.

Karabaja

12:00 am on Aug 20, 2006 (gmt 0)

10+ Year Member



Thx guys. But I got a reply on a linux forum and managed to sort it out. Site turned out quite good.

And I am not sure how would I use this:

<iframe><pre>stuff here</pre></iframe>

Since I have to specify <iframe src="myfile.txt">

Anyway I had to write a long long script combining echo and sed commands to wrapt txt files with pre tags. But as long as it is sorted in the end I am happy.

evaddnomaid

9:04 am on Aug 21, 2006 (gmt 0)

10+ Year Member



Glad to hear you created a solution! Shell scripting can solve so many problems.

If it is not too much trouble, could you post your shell script here for everyone to learn from?

Karabaja

2:23 pm on Aug 21, 2006 (gmt 0)

10+ Year Member



Sure, although the one I used is not that practical if you got too many files to process but I got suggestion about easier solution on one forum after I already wrote my long script.

Here is what I used.


echo "</pre>" >> file.txt

this inserts </pre> at the end of txt file. In combination with this:


sed 's/word/<pre>/g' file.txt > /destination/folder/file.txt

to replace first word in file with <pre> and save output to where I need the files.

Here is an easier solution that inserts tags at the beginning and the end of all txt files in specified folder without replacing any words in the files.


files="ls /data/*.txt"
echo '<pre>' > /tmp/head
echo '</pre>' > /tmp/tail

for i in $files
do
out="/tmp/out.$$"
cat /tmp/head $i /tmp/tail > $out
cp $out $i
rm -f $out
done

rm -f /tmp/head /tmp/tail