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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
03-26-2006, 12:55 PM
|
#1
|
Member
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709
Rep:
|
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
|
|
|
03-26-2006, 01:01 PM
|
#2
|
Senior Member
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290
|
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.
|
|
|
03-26-2006, 02:09 PM
|
#3
|
Member
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709
Original Poster
Rep:
|
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
|
|
|
03-26-2006, 11:55 PM
|
#4
|
Senior Member
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290
|
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.
|
|
|
All times are GMT -5. The time now is 09:45 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|