LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-29-2008, 04:02 AM   #1
saavik
Member
 
Registered: Nov 2001
Location: NRW, Germany
Distribution: SLES / FC/ OES / CentOS
Posts: 614

Rep: Reputation: 32
insert at the top of a file


Hello!

I need to insert a text at the top of a file ( in BASH ).

How can that be done ?

I need something like

# echo test >> test.txt

but at the top of the file.

THANKS
 
Old 08-29-2008, 04:11 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
echo test > test
echo test.txt >> test
mv test test.txt
 
Old 08-29-2008, 04:13 AM   #3
saavik
Member
 
Registered: Nov 2001
Location: NRW, Germany
Distribution: SLES / FC/ OES / CentOS
Posts: 614

Original Poster
Rep: Reputation: 32
No, not really!

I have a variable lets say $a which I have to insert at the top of an existing file.
 
Old 08-29-2008, 04:22 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
ghostdog, maybe you mean
Code:
echo test > test
cat test.txt >> test
mv test test.txt
saavik, ghostdog74 just gave you one of the infinite solutions. You can try to figure it out. If a string is the value of a shell variable, don't you think that the following are equivalent?
Code:
echo some_text
Code:
a=some_text
echo $a
 
Old 08-29-2008, 05:38 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by colucix View Post
ghostdog, maybe you mean
Code:
echo test > test
cat test.txt >> test
mv test test.txt
oh yes, my bad. thanks
 
Old 08-29-2008, 05:44 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
How about:
Code:
cat test.txt test > test.new
mv test.new test
 
Old 08-29-2008, 04:20 PM   #7
fberbert
LQ Newbie
 
Registered: May 2004
Location: Brazil
Distribution: Debian
Posts: 7

Rep: Reputation: 0
Hi,

Try this:

echo -e "text in the top\n`cat oldfile.txt`" > newfile.txt

For keeping the same filename:

echo -e "text in the top\n`cat oldfile.txt`" > newfile.txt && mv newfile.txt oldfile.txt

---
Fabio Berbert de Paula
http://www.vivaolinux.com.br

Last edited by fberbert; 08-29-2008 at 04:24 PM.
 
Old 08-29-2008, 06:29 PM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
This modifies the file without using a second file:-
Code:
echo "test$( < test.txt)" > test.txt
 
  


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
how to insert into a file? bbmak Linux - General 2 09-02-2007 12:13 PM
Insert Data into File laurasong Linux - General 3 08-17-2007 03:25 PM
Insert date and timestamp Into File name petenyce Linux - Newbie 9 10-13-2005 12:16 PM
insert something in the middle of file using ansi c? Thinking Programming 5 07-26-2005 01:47 PM
How can I insert a .GIF file into my post? costasm LQ Suggestions & Feedback 4 10-15-2003 06:04 PM

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

All times are GMT -5. The time now is 03:27 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