bash - sed:在字符串之间添加新行

标签 bash sed ls

我使用 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/

相关文章:

linux - 输入 'ls' 命令时,我得到 'zsh: command not found: gls'

regex - 使用 sed 删除 html 注释标签

ios - 从 plutil 获取信息

linux - 如何使用 bash 从大文件中提取所有以特定字符开头的单词?

regex - 使用 SED 删除不匹配的行

unix - 如何通过排除某些文件扩展名来搜索 unix 中的一个或多个目录

合并 grep 和 ls -lrt 输出的 Linux 命令

linux - 使用 bash 脚本注释掉文件中的行

linux - Shell脚本双引号

linux - Linux 中匹配数据然后打印某些数据的最佳命令