How to add a text to first line of a text file?
Hello! I need to add a line to text files. Basically, I need to add "<pre>" tag to the first line of text files to make them viewable on web browsers. I guess I could create a file with "<pre>" tag on it first and then concatenate the text file to it. But this needs to be done to hundreds of text files every morning. And I was wondering if there is a way that will take less time and cpu power. Thanks for your input!
for example, a text file could be... apples oranges bananas pears I need to change this to... <pre> apples oranges bananas pears |
Ah, kill me, I've no idea. I'd probably do it this way:
Code:
echo '<pre>'.`cat your_file` > your_file |
You can try with sed, inserting the string before the first line of the file and by editing the file in place with option -i
Code:
sed -i 1i"<pre>" filename |
cron job
If it needs to be done every morning then create a shell script with any of the previously mentioned solutions that work for you, and install it as a cron job.
I wouldn't call such a routine a CPU eater. Not even for a few hundred files. The script could finish the job within seconds. I do not assume you have 100MB text files to display in a web browser. Server issues |
Thanks for all your replies! It seems that a solution with echo command combine all lines to a one line. Is there a way to get around this?
$echo '<pre>' . `cat fruits` > myfruits $cat fruits apples oranges bananas pears $cat myfruits <pre> . apples oranges bananas pears $ With sed solution, I get a long error message as shown below. $sed -i 1i"<pre>" fruits sed: invalid option -- i Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]... -n, --quiet, --silent suppress automatic printing of pattern space -e script, --expression=script add the script to the commands to be executed -f script-file, --file=script-file add the contents of script-file to the commands to be executed --help display this help and exit -V, --version output version information and exit If no -e, --expression, -f, or --file option is given, then the first non-option argument is taken as the sed script to interpret. All remaining arguments are names of input files; if no input files are specified, then the standard input is read. E-mail bug reports to: bug-gnu-utils@gnu.org . Be sure to include the word ``sed'' somewhere in the ``Subject:'' field. |
Quote:
Do NOT use `cat ...`, this is completely retarded, you're cat'ing the file to stdout, then echoing it back to the file, thus taking twice as long as it should (maybe Maligree works for MS? ;)). |
if its showing in a webpage you could create a php page to do it for you.
<?php include("textfile.php"); ?> I think it will work with .txt files as well. and <pre> tag could be included in the main page. You could also get fancy with the shell script like #!/bin/bash for i in $(cat *.txt); do echo "<pre>" >> page.htm echo "$i" >> page.htm done exit I have not written a shell script for a long time so the above may be completely wrong, but its an idea. Shell script would be the best if these files are going to be changing daily. |
All times are GMT -5. The time now is 12:56 PM. |