cat without line breaks: why does tr '\n' not work?

Issue

I generated 1000 output files containing a single line with (mistakenly) no line break at the end, so that

cat filnename_* > outfile

generates a file with a single line. I attempted to remedy this using

cat filename_* | tr '\n' ' ' > outfile

but I get exactly the same result – a file with a single line of output. Why doesn’t the latter code (which ought to add a line break for each filename_* file) accomplish what I’m trying to do?

Solution

If all your files are missing the final linefeed then you can use sed for adding it on the fly:

# with GNU sed
sed '$s/$/\n/' filnename_* > outfile

# with standard sed and bash, zsh, etc...
sed $'$s/$/\\\n/' filnename_* > outfile

# with standard sed and a POSIX shell
sed '$s/$/\
/' filnename_* > outfile

Answered By – Fravadona

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published