LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
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,290

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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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