标签:sed

浏览模式: 标准 | 列表 | 图文
invalid command code ., despite escaping periods, using sed

invalid command code ., despite escaping periods, using sed

linux上面,如果替换文件中的内容,其实还是比较方便的。比如:sed -i 's/xxx/yyy/g' xxx.txt

然而同样的命令,如果放到mac下面,就会报:sed: 1: "xxx.txt": invalid command code o
到stackoverflow.com就会发现已经有人回复了:https://stackoverflow.com/questions/19456518/invalid-command-code-despite-escaping-periods-using-sed
If you are on a OS X, this probably has nothing to do with the sed command. On the OSX version of sed, the -i option expects an extension argument so your command is actually parsed as the extension argument and the file path is interpreted as the command code.

Try adding the -e argument explicitly and giving '' as argument to -i:

find ./ -type f -exec sed -i '' -e "s/192.168.20.1/new.domain.com/" {} \;
See this.
所以上面的代码就改为:sed -i '' -e 's/xxx/yyy/g' xxx.txt ,搞定
参考:
1、https://stackoverflow.com/questions/19456518/invalid-command-code-despite-escaping-periods-using-sed
2、https://stackoverflow.com/questions/7573368/in-place-edits-with-sed-on-os-x
linux 批量重命名文件

linux 批量重命名文件

其实说白了就是利用sed,但怎么个用法,确实是有点想法,毕竟sed的功能实在太强了。

单一文件重命名就太简单了,mv一下就全来了。批量怎么办?
OK,一句话:
for i in `ls`; do mv -f $i `echo $i | sed 's/????/?????/'`; done
看到没。其实就是一个for in,然后 mv 一下。只是mv的蚨,用sed进行了改名,用上了管道,echo 等。
接着来:
1、改文件的首字母为a:
for i in `ls`; do mv -f $i `echo $i | sed 's/^./a/'`; done
2、改文件的末字母为a
for i in `ls`; do mv -f $i `echo $i | sed 's/.$/a/'`; done
3、文件名加前缀:
for i in `ls`; do mv -f $i `echo "prefix_"$i`; done
4、文件名小写变大写
for i in `ls`; do mv -f $i `echo $i | tr a-z A-Z`; done
顾名思议,大写变小写就是将A-Z和a-z换一个位置
5、改指定字符为其他(如后缀名)
for i in `ls`; do mv -f $i `echo $i | sed 's/.html/.php/'`; done
更多技巧,请参考sed的用法