OS X下使用find和sed来批量修改文件的时候的问题。
需求是这样的,有文件file1.txt ,文件file2.txt。
都只有一行内容:
zx:hexojs zx$ cat file1.txt
file1.txt
zx:hexojs zx$ cat file2.txt
file2.txt
需要在每个txt文件中插入几行。
首先,使用find 查找txt文件
zx:hexojs zx$ find . -d 1 -name "*.txt"
./file1.txt
./file2.txt
然后使用sed来插入文件内容
sed -i '' -e '1i \
A \
B ' file3.txt
结合起来使用
zx:hexojs zx$ find . -d 1 -name "*.txt" -print0 | xargs -0 sed -i '' -e '1i \
> C \
> D '
但是,最后只有文件1里被插入了,文件2里没有被修改,怎么回事?
zx:hexojs zx$ cat *.txt
C
D
file1.txt
file2.txt
GNU版本和FreeBSD版本的sed在处理in-place的修改操作时是不一样的
GNU版本会将每个文件分别处理,每个文件都有第1行
而FreeBSD版本会将多个文件视作一个流,只有一个第1行