Adding end of line character in the last record

Need to add end of line character to last record in a fixed width file.

When i take the record length of each line, for all the records it gives example like 200 except the last line as 199.

Due to this my other script fails.

Please help me on this.

I think this will add newline to end of last line:

sed "$ s/$/\n/" infile > outfile
1 Like

Do you mean the last line has no trailing newline? In that case you may or may not be able to use sed some can, but some cannot handle that. Try

printf "\n" >> file

--
@hanson44: most sed s cannot handle \n in the replacement part of the substitute command (and they will write a literal "n")

1 Like

sed does read the last line, even if no EOL, at least with GNU sed.
I guess I'm used to GNU sed or other modern version of sed.
But you have a good point. And your solution is 100% better.

$ echo -n x > temp.x
$ cat temp.x
x$sed "$ s/$/\n/" temp.x
x
$

Thanks for both. Its is workinf fine.