LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-19-2016, 02:08 PM   #1
tinkerer
LQ Newbie
 
Registered: Apr 2016
Posts: 2

Rep: Reputation: Disabled
How can I prepend lines to beginning of file


hello

I know how to append lines to the end of files with a >> characters. Is there a way to add lines to the begiining of a file. thanks
 
Old 04-19-2016, 02:27 PM   #2
themanwhowas
Member
 
Registered: Nov 2005
Distribution: CentOS 5, Fedora 23
Posts: 218

Rep: Reputation: 29
sed -i '1 i\some text' /home/me/somefile.txt
 
Old 04-20-2016, 01:24 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Or perhaps

cat file_with_more_lines oldfile >newfile
mv newfile oldfile
 
Old 04-20-2016, 04:42 PM   #4
GaryWeaton
LQ Newbie
 
Registered: Apr 2016
Posts: 7

Rep: Reputation: Disabled
Another variant of cat with echo

Code:
echo "Put your text here" | cat - file > newfile
 
Old 04-20-2016, 07:19 PM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Thats a good one. I was originally going to do a "(echo "Put your text here"; cat file) > newfile"

Yours is much better.

Last edited by jpollard; 04-20-2016 at 07:20 PM.
 
Old 04-20-2016, 10:15 PM   #6
GaryWeaton
LQ Newbie
 
Registered: Apr 2016
Posts: 7

Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
Thats a good one. I was originally going to do a "(echo "Put your text here"; cat file) > newfile"

Yours is much better.
Thanks jpollard. Even though all 3 codes will do the same task for OP, we can agree themanwhowas code is best as it doesn't create another file for the new changes.

Last edited by GaryWeaton; 04-20-2016 at 10:19 PM.
 
Old 04-20-2016, 10:35 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by GaryWeaton View Post
Thanks jpollard. Even though all 3 codes will do the same task for OP, we can agree themanwhowas code is best as it doesn't create another file for the new changes.
Internally, sed does the same... but with more work as it has to create the temporary file, then copy the result back into the original file.

The only advantage is that the inode number stays the same, as does any permissions on the file.
 
Old 04-21-2016, 09:07 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,610
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
I prefer "post #4," but with the additional step of moving (renaming ...) the old-file to an alternate name before moving the new-file into its place.

The advantage of this strategy is that it can be restarted if something goes wrong along the way. You create a new version of the output first, without altering any of the inputs, nor the existing version of the output. Briefly, the two exist side-by-side. Then, after moving the old-version out of the way (without destroying it ...), you finish the job. But, up to a point, if you then say " !!", your foot doesn't have a hole in it. You can compare the before-and-after files to make sure you didn't screw-up, and you can gracefully recover if when you do. (You can even archive the old versions, indefinitely, for statistical or auditing purposes.)

Last edited by sundialsvcs; 04-21-2016 at 09:08 AM.
 
Old 04-21-2016, 09:31 AM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Yes.

Without the need to keep old versions, the rename becomes an atomic operation. Other accesses get either the old file, or the new - but never a mix.
 
Old 04-21-2016, 09:42 AM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,770

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
Quote:
Originally Posted by jpollard View Post
Internally, sed does the same... but with more work as it has to create the temporary file, then copy the result back into the original file.

The only advantage is that the inode number stays the same, as does any permissions on the file.
No, the inode number does not stay the same. After sed creates the new file and sets its permissions, it simply renames it over the original. Unlike copying, renaming is an atomic operation and will never leave duplicate or partially copied files.

You can use the "--copy" option to preserve the inode number and its hard links.

Last edited by rknichols; 04-21-2016 at 09:44 AM.
 
Old 04-21-2016, 09:52 AM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
oops. Sorry about that.
 
Old 04-21-2016, 10:22 PM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,610
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
Obviously, any "real" implementation of this idea would have to properly deal with concurrency, e.g. through a combination of exclusive file-locking [i](which Unix/Linux might not have ...) and sentinel-files. You would need to somehow know that someone was not already accessing the data, and that no one attempts to do so while the file-state is inconsistent. This is your responsibility to take care of, by whatever means are appropriate to your use-case. (It might be that there is no pragmatic need to do anything special at all.)
 
Old 04-22-2016, 05:28 AM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Which is why using "mv newname oldnazme" works.
 
Old 04-22-2016, 11:22 AM   #14
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,770

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
Quote:
Originally Posted by jpollard View Post
Which is why using "mv newname oldnazme" works.
No, it is entirely possible for another process to make changes to the file between the time that sed reads it and the time it renames the new file over the old. In fact, if another process is holding that old file open, it will still be accessing the old, deleted file even after the rename. It's an issue that is nowhere near as easily solved as you seem to think.
 
Old 04-22-2016, 12:08 PM   #15
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Which makes it atomic. Either you get the old version, or you get the new one.

Not a mix of old+new.

Directory operations are atomic.
 
  


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
[SOLVED] remove certain lines from file based on start of line except beginning and ending nwalsh88 Linux - Newbie 5 02-20-2013 10:20 AM
[SOLVED] add text at the beginning and at the end of lines in a file smithy2010 Linux - Newbie 6 07-22-2012 04:07 AM
[SOLVED] To skip lines from the beginning of a file using FORTRAN commands saj Programming 2 05-31-2011 11:51 PM
Delete specific lines of a file beginning with a certain letter docaia Programming 4 08-24-2008 11:04 PM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM

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

All times are GMT -5. The time now is 07:56 AM.

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