I have a file like the following:a,b,c,d,e,f1,2,3,4,5,67,8,9,0,1,2I need to join the last two lines with a comma, like so:a,b,c,d,e,f1,2,3,4,5,6,7,8,9,0,1,2The problem is that this is all inside a loop that will add two new lines to that file, saya,b,c,d,e,f1,2,3,4,5,6,7,8,9,0,1,20,9,8,7,6,54,3,2,1,0,9And now I need to do the same to that last line for the file to end likea,b,c,d,e,f1,2,3,4,5,6,7,8,9,0,1,20,9,8,7,6,5,4,3,2,1,0,9So, in only a few words, I need to replace the last new line of a text file with a comma, but only the last line, as lines are being added.I've tried several things but they don't work. The last one was sed '$!N;s/\n/,/' dna.data, but it applies to all lines with a newline.Any ideas?
sed: replacing newline at a given line (or last line)
sed
If the file does not end with a newline, you just need to address the last substitute command (use the -i flag of sed to change the file inplace):$ sed '$!N;$s/\n/,/' dna.dataa,b,c,d,e,f1,2,3,4,5,6,7,8,9,0,1,20,9,8,7,6,5,4,3,2,1,0,9  If the file ends with a newline, you can use the editor ex:$ ex dna.data$-1s/\n/,/1,2,3,4,5,6,7,8,9,0,1,2xor in one lineecho -e '$-1s/\\n/,/\nx' | ex dna.data