Append newline to file bash
To append a line using the tee command, we use the -a option. We can then pass the input from a heredoc as:. Once you execute the command, a tee will display the contents and append them to the specified file.
Using the cat command to append to a file is similar to using the tee command. However, we use the append redirection symbols to redirect the output. My name is John and am a fellow geek like you. Asked 10 years, 11 months ago. Active 10 years, 5 months ago. Viewed 2k times. Here is how I read a file row by row: while read ROW do It is easy to solve this in the latter syntax, by echoing just a newline: cat file; echo while read ROW do Narcolessico Narcolessico 1, 3 3 gold badges 20 20 silver badges 18 18 bronze badges.
I specify: with "losing env variables" I mean that the changes made inside the loop are not visible outside e. Of course from within the loop I can still access the inherited variables. Maybe i am not simulating the result correctly, but what version of bash are you using? I am using bash4 and do have a problem reading the last line, with and without the last new line. Add a comment. Active Oldest Votes.
Appending newline character End of File. Hi Gurus, Need help. I'm a beginner in Unix. How to add newline character at end of file? Hi All, I have following piece of code in UNIX C Shell script and I want to add one more command which can add newline at the end of file only if there is no newline character exists. Hi all, i have to append a record at the end of the file a file which is already with some records.
Hi, i want to append a character ' ' at end of each line of a file abc. Append text at end of the first line in a file. Hi I need to append some text end of the first line in a file. I give the arg "d". Please help me out this 7 Replies.
I need the line printed with echo to append to eof of to exactly line, am i able to do that? If the last byte of the file is a newline, tail returns it, then command substitution strips it; the result is an empty string. The -n test fails and echo does not run. If the file is empty, the result of the command substitution is also an empty string, and again echo does not run.
This is desirable, because an empty file is not an invalid text file, nor is it equivalent to a non-empty text file with an empty line. Another solution using ed. It essentially works opening the file for editing through a script, the script is the single w command, that write the file back to disk. It is based on this sentence found in ed 1 man page:. Is really fast. Other solutions take a long time:. If you need a solution portable to yash and all other shells listed above , it may get a bit more complex:.
The fastest way to test if the last byte of a file is a newline is to read only that last byte. That could be done with tail -c1 file. However, the simplistic way to test if the byte value is a new line, depending on the shell usual removal of a trailing new line inside a command expansion fails for example in yash, when the last character in the file is an UTF-8 value.
The correct, POSIX-compliant, all reasonable shells way to find if the last byte of a file is a new line is to use either xxd or hexdump:. Then, comparing the output of above to 0A will provide a robust test.
It is useful to avoid adding a new line to an otherwise empty file. File that will fail to provide a last character of 0A , of course:. Short and sweet. This takes very little time as it just reads the the last byte seek to EOF. It does not matter if the file is big.
Then only add one byte if needed. And it need only read the input file through the one time to get it right. At least in the GNU versions, simply grep '' or awk 1 canonicalizes its input, adding a final newline if not already present. They do copy the file in the process, which takes time if large but source shouldn't be too large to read anyway?
Although it doesn't directly answer the question, here is a related script I wrote to detect files which do not end in newline. It is very fast. The perl script reads a list of optionally sorted file names from stdin and for every file it reads the last byte to determine if the file ends in a newline or not. It is very fast because it avoids reading the entire contents of each file. It outputs one line for each file it reads, prefixed with "error:" if some kind of error occurs, "empty:" if the file is empty doesn't end with newline!
Note: the script doesn't handle file names which contain newlines. If you're on a GNU or BSD system, you could handle all possible file names by adding -print0 to find, -z to sort, and -0 to perl, like this:. Of course, you'd still have to come up with a way of encoding the file names with newlines in the output left as an exercise for the reader.
The output could be filtered, if desired, to append a newline to those files which don't have one, most simply with.
0コメント