sed: adding a new line between strings

60 views Asked by At

I am using the combination of the ls + sed to make a list of the images in the folder and create output file of scpeficic format where each line separated by the empty line etc

ls ${blocks}/*png | sed 's/^/![caption](/;s/$/)\n/' > ${blocks}/out

The problem is that, inspite of the \n in the end sed does not add a "new line" after each string.

also would it be better to use find instead of ls for big number of png images?

2

There are 2 answers

4
anubhava On BEST ANSWER

Parsing output of ls can be error prone and must be avoided. Moreover bsd sed doesn't interpret \n as newline like you're using and emits a literal n in the substitution.

However, you don't even need ls | sed here, just use this for loop:

for f in "$blocks"/*.png; do
   printf '![caption](%s)\n\n' "$f"
done > "$blocks"/out

Or better just printf:

printf '![caption](%s)\n\n' "$blocks"/*.png
5
Renaud Pacalet On

You can try:

find "$blocks" -type f -name '*.png' -printf "![caption](%p)\n\n"