LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How can I prepend lines to beginning of file (https://www.linuxquestions.org/questions/linux-newbie-8/how-can-i-prepend-lines-to-beginning-of-file-4175577854/)

tinkerer 04-19-2016 02:08 PM

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

themanwhowas 04-19-2016 02:27 PM

sed -i '1 i\some text' /home/me/somefile.txt

jpollard 04-20-2016 01:24 PM

Or perhaps

cat file_with_more_lines oldfile >newfile
mv newfile oldfile

GaryWeaton 04-20-2016 04:42 PM

Another variant of cat with echo

Code:

echo "Put your text here" | cat - file > newfile

jpollard 04-20-2016 07:19 PM

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

Yours is much better.

GaryWeaton 04-20-2016 10:15 PM

Quote:

Originally Posted by jpollard (Post 5534251)
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.

jpollard 04-20-2016 10:35 PM

Quote:

Originally Posted by GaryWeaton (Post 5534309)
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.

sundialsvcs 04-21-2016 09:07 AM

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 ":eek: !!", 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.)

jpollard 04-21-2016 09:31 AM

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.

rknichols 04-21-2016 09:42 AM

Quote:

Originally Posted by jpollard (Post 5534312)
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.

jpollard 04-21-2016 09:52 AM

oops. Sorry about that.

sundialsvcs 04-21-2016 10:22 PM

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.)

jpollard 04-22-2016 05:28 AM

Which is why using "mv newname oldnazme" works.

rknichols 04-22-2016 11:22 AM

Quote:

Originally Posted by jpollard (Post 5534942)
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.

jpollard 04-22-2016 12:08 PM

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.


All times are GMT -5. The time now is 01:12 AM.