cat
cannot output data that does not exist in the files. If a file is empty, it does not even have a newline character to provide an empty line as output.
You could make sure that the files contained at least a single newline character.
This is how you use GNU awk
to ensure that (this modifies the empty files):
awk 'ENDFILE { if (FNR == 0) printf("\n") >>FILENAME }' 2[0-8]_SumActive.txt
The ENDFILE
block will be executed after finishing reading any of the files. If FNR
is zero, we never saw any lines in the file, so we insert a single newline into it. The script then continues with the next file.
You can then use cat
as you did in the question.
Alternatively, without changing the files, using GNU awk
instead of cat
:
awk 'ENDFILE { if (FNR == 0) printf("\n") } 1' 2[0-8]_SumActive.txt
This does the same kind of detection of empty files as above, but prints the newline to standard output rather than to the file. The 1
at the end could be replaced by { print }
and will cause all data in the non-empty files to be outputted.
Alternatively, a shell loop (should work in any POSIX shell):
for name in ./2[0-8]_SumActive.txt; do
if [ -s "$name" ]; then
cat "$name"
else
printf '\n'
fi
done
The -s
test will be true if the file exists and has a size greater than zero.
If you want the literal string [Blank]
to be outputted for empty files, simply insert that string in front of \n
in the calls to printf
above (this will also work in the awk
code).
[Blank]
did you mean those 7 characters or did you intend to indicate an empty line?