Unix append to file without new line
Assuming that the file does not already end in a newline and you simply want to append some more text without adding one, you can use the -n argument, e. However, some UNIX systems do not provide this option; if that is the case you can use printf , e. Do not print the trailing newline character. Applications aiming for maximum portability are strongly encouraged to use printf 1 to suppress the newline character.
If you end up with abc on its own line, that means that the newline before abc was already present in file. Note that it is perfectly normal for a text file to end in a newline.
Instead you need a tool that is capable of modifying an existing file. For example, you can use sed :. Linux's sed command has a built-in feature to automate this create-new-file-and-replace sequence, so you can shorten that to.
Text processing programs may or may not cope with this character. I don't think it's possible with echo command, use the following sed approach instead:.
If you have the truncate command and your text file has NL as its last character you can remove it and then append your text like this:. Note that truncate cares nothing about file content, and in this example simply reduces the file size by one byte. If your last character is not a single byte, ie it's a multi-byte "wide" character then you will introduce corruption.
What you want is to add it at the end of the last line, so just before the delimiter of that last line, so just before the last character of the file.
Though for a more exact equivalent, you'd also need to print an error message upon failure to seek:. Probably a UUOC but you could also do:.
This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. Sign up to join this community.
The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Try something like this: I will add Eon at the end of the line Code :. Join Date: Jan Last edited by Franklin52; at AM.. Reason: Please use code tags for code and data samples, thank you. Reason: Code tags. It can be written in sed one liner. Here is how: Code :. Join Date: Nov Using sed to find and append or insert on SAME line.
Hi, I posted in another section, but no reply yet. Hello, I have to add a new line at the end of a File on Solaris-System: I think my script should be right, because I evaluated it to other threads. However the script does not what I am expected it should do. My file might look like this: Line1 Line2 Line3 And my script could I have the a group file and my ftp group line looks like this How to append line with sed?
Input: gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly Output should be: gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly How can it be done with sed? Sed : identify a pattern and append a word at the end of a line. Hello to all, On aix, I want to identify a term on a line in a file and then add a word at the end of the line identified. Downvote because answer is already exists here.
Why do you downvote the correct answer then, cOrp, and not the duplicate question? Avinash Raj Avinash Raj The Overflow Blog.
Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked Related 2. Hot Network Questions.
0コメント