LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-22-2008, 02:55 PM   #1
oskeewow
LQ Newbie
 
Registered: Apr 2008
Location: Greater Chicago
Distribution: Centos, SuSE, RHEL..etc..
Posts: 9

Rep: Reputation: 0
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
 
Old 04-22-2008, 03:08 PM   #2
Maligree
Member
 
Registered: Mar 2008
Distribution: Gentoo, CentOS, Fedora, Arch
Posts: 231
Blog Entries: 1

Rep: Reputation: 42
Ah, kill me, I've no idea. I'd probably do it this way:
Code:
echo '<pre>'.`cat your_file` > your_file
But that's pretty much what you're trying to avoid.. Huh. Is it really THAT much of a CPU eater?

Last edited by Maligree; 04-22-2008 at 03:12 PM.
 
Old 04-22-2008, 03:13 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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
 
Old 04-22-2008, 08:05 PM   #4
marquardl
Member
 
Registered: Apr 2008
Posts: 100

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

Last edited by marquardl; 05-01-2008 at 04:08 AM.
 
Old 04-23-2008, 10:42 AM   #5
oskeewow
LQ Newbie
 
Registered: Apr 2008
Location: Greater Chicago
Distribution: Centos, SuSE, RHEL..etc..
Posts: 9

Original Poster
Rep: Reputation: 0
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.
 
Old 04-23-2008, 11:29 AM   #6
ararus
Member
 
Registered: Mar 2008
Location: UK
Distribution: Slackware
Posts: 56

Rep: Reputation: 15
Quote:
sed: invalid option -- i
If your sed doesn't have the -i option, then just do it explicitly. As you suggested, just create a file with '<pre>' then cat the other file onto it. This is *no* easier way to do it, you can't prepend data onto a file without rewriting it.

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? ).

Last edited by ararus; 04-23-2008 at 11:32 AM.
 
Old 04-23-2008, 12:40 PM   #7
jei
LQ Newbie
 
Registered: Jul 2006
Posts: 29

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

Last edited by jei; 04-23-2008 at 12:43 PM.
 
  


Reply

Tags
bash, edit, linux, script


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
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 07:54 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM


All times are GMT -5. The time now is 04:43 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration