LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 03-26-2006, 12:55 PM   #1
linuxmandrake
Member
 
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709

Rep: Reputation: 30
writing text to the beginning of a file


I have these files which have random stuff in them but I want all of them to have "steven" as their very first line. How can I do that to all files with a .txt extension
 
Old 03-26-2006, 01:01 PM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,160

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
Linux and Unix systems do not use file extensions the way Windows does -- they don't matter.The following willwork on all text files (though I use *.txt as a fileglob to apply the file operation to all files ending in .txt, but you could use any other expression). The actual command is:

sed -i.bak -e 'i steven\' *.txt

All of the files will be backed up (with a .bak extension added) before they are edited in case something goes wrong. I'd suggest reading up on sed to understand how what I did works.
 
Old 03-26-2006, 02:09 PM   #3
linuxmandrake
Member
 
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709

Original Poster
Rep: Reputation: 30
it doesn't work

original file
a
b
c
d


after executing command
steven
a
steven
b
steven
c
steven
d

what i want is
steven
a
b
c
d
 
Old 03-26-2006, 11:55 PM   #4
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,160

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
Oops, I forgot the line specifier, correct command is:

sed -i.bak -e '1 i steven\' *.txt

The leading 1 tells sed to perform the operation only on the first line of the file.
 
  


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
vb.net writing to a specific line in a text file mrobertson Programming 3 12-30-2005 11:24 PM
Writing to a specific line in a text file mrobertson Programming 6 12-30-2005 05:02 PM
vb.net reading and writing to a text file simultaneously mrobertson Programming 3 09-08-2005 01:30 PM
Writing to a text file vb.net mrobertson Programming 5 08-04-2005 01:37 PM
How to delete a file beginning with '-'? zoomzoom Linux - General 2 02-02-2004 11:01 AM


All times are GMT -5. The time now is 04:36 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration