Quote:
Originally Posted by
Scrutinizer
Hi Don, I see where you are going, I may have read the question wrong, I interpreted it as "add a carriage return is it is missing at the end of a line (which ends in linefeed)". So for that, my suggestion does not use extensions - GNU or otherwise - to the standard. I did not mean to also correct a missing newline at the end of the file.
It may be good to note that even though the latter case is not defined by the standards, every implementation of awk on every OS I know does read the unterminated last line and adds the newline at the end of the file, so IMO this behavior cannot be labeled as a GNU extension.
Hi Scrutinizer,
Note that the title of this thread (and the description in post #1 in this thread) says "
add Carriage Return and Line Feed"; not "
add Carriage Return before Line Feed". The description to me sounded like the input file is a DOS text file with <carriage-return><line-feed>(AKA <newline>) line
separators that the submitter wants to turn into a DOS text file with complete DOS lines including a <carriage-return><line-feed> terminator at the end of the last line.
The standards say that the behavior is only defined for
awk if all input files are text files. It doesn't make any exception to that requirement for files that are text files except for a partial final line. (The standards do make that exception for the
ex utility.)
I am almost positive that PWB UNIX, UNIX System III, UNIX System V, and the Solaris
/usr/xpg4/bin versions of
awk (at least through early Solaris 10 updates) dropped partial lines without feeding them (with or without an added <newline>) through the script. But, I don't have access to any of those systems to verify it as this point. BSD/macOS
awk does add a <newline> terminator to a partial line at the end of a file so it is an extension that is provided by GNU and other systems.