I have been noticing behaviour with end of line '\n' I don't understand and therefore I have come up with the following "test". I am on Mac OS X.
I have a file with:
one two
three four five
six seven
Then I run
x=$(awk '{print}' filename.txt)
and
echo "$x"
prints correctly (i.e. the content of the file as above).
However, when I do
y=$(echo $x | tr "\n" "N")
(I do mean to translate new line '\n' into a capital 'N', just to see what happens), then I get
one two three four five six sevenN
It seems that all occurrences of '\n', but the last, are detected (because the output is on one line) but not replaced with 'N', while the replacement occurs for the last instance.
Is the last character (at the end of the file) different from the other new line characters?
I don't understand what is going on.
EDIT
I can think of one difference between the last line and the others. awk
sees one new line character \n
at the end of all lines except the last one, and when it prints it adds another one such new line. So what I need is to replace a double new line with N
.
On the other hand, why do I not see two N
s?
There is only one '\n' at the end of the last line, and it is replaced with N
.
Is this getting closer to an explanation?