我使用 ls + sed 的组合来创建文件夹中的图像列表,并创建 scpeficic 格式的输出文件,其中每行由空行等分隔
ls ${blocks}/*png | sed 's/^/![caption](/;s/$/)\n/' > ${blocks}/out
问题是,尽管最后有\n sed 并没有在每个字符串后添加“新行”。
对于大量 png 图像,使用 find 而不是 ls 会更好吗?
最佳答案
解析 ls
的输出可能容易出错,必须避免。此外,bsd sed
不会像您使用的那样将 \n
解释为换行符,并在替换中发出文字 n
。
但是,您甚至不需要 ls | sed
在这里,只需使用这个 for 循环:
for f in "$blocks"/*.png; do
printf '![caption](%s)\n\n' "$f"
done > "$blocks"/out
或者更好的是printf
:
printf '![caption](%s)\n\n' "$blocks"/*.png
关于bash - sed:在字符串之间添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77302493/