I have a small Bash script that takes all Markdown files of a directory, and merge them into one like this:
for f in *.md; do (cat "${f}";) >> output.md; done
It works well. Now I'd like to add the string "\newline" between each document, something like this:
for f in *.md; do (cat "${f}";) + "\newline" >> output.md; done
How can I do that? The above code obviously doesn't work.
If you want the literal string "\newline"
, try this:
for f in *.md; do cat "$f"; echo "\newline"; done > output.md
This assumes that output.md
doesn't already exist. If it does (and you want to include its contents in the final output) you could do:
for f in *.md; do cat "$f"; echo "\newline"; done > out && mv out output.md
This prevents the error cat: output.md: input file is output file
.
If you want to overwrite it, you should just rm
it before you start.
You can do:
for f in *.md; do cat "${f}"; echo; done > output.md
You can add an echo
command to add a newline. To improve performance I would recommend to move the write >
outside the for
loop to prevent reading and writing of file at every iteration.
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