You can do:
for f in *.txt; do (cat "${f}"; echo) >> finalfile.txt; done
Make sure the file finalfile.txt
does not exist before you run the above command.
If you are allowed to use awk
you can do:
awk 'FNR==1{print ""}1' *.txt > finalfile.txt
If you have few enough files that you can list each one, then you can use process substitution in Bash, inserting a newline between each pair of files:
cat File1.txt <(echo) File2.txt <(echo) File3.txt > finalfile.txt
If it were me doing it I'd use sed:
sed -e '$s/$/\n/' -s *.txt > finalfile.txt
In this sed pattern $ has two meanings, firstly it matches the last line number only (as a range of lines to apply a pattern on) and secondly it matches the end of the line in the substitution pattern.
If your version of sed doesn't have -s
(process input files separately) you can do it all as a loop though:
for f in *.txt ; do sed -e '$s/$/\n/' $f ; done > finalfile.txt
This works in Bash:
for f in *.txt; do cat $f; echo; done
In contrast to answers with >>
(append), the output of this command can be piped into other programs.
Examples:
for f in File*.txt; do cat $f; echo; done > finalfile.txt
(for ... done) > finalfile.txt
(parens are optional)for ... done | less
(piping into less)for ... done | head -n -1
(this strips off the trailing blank line)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With