Think before you speak, read before you think.

grep, sed 结合查找替换某目录下所有文件

by

in

查找:

grep “要找的字符串” -rl 目录,例如

grep -rl "hello.world"  .

替换:sed -i “s/要查找的字符串/替换字符串/g” `grep “要查找的字符串” -rl 目录`,例如

sed -i "s/friends/girls/g" `grep -rl "hello.world"  .`

注意:

替换中的 grep 命令要被 TAB 键上边的那个引号(反引号)包起来

例二:

清理配置文件中的注释,即 # 开头的
查看注释开头有注释的行

grep -rn '^\s*#' nginx.conf

显示有注释开头的文件

grep -rl '^\s*#' .

删除包含有注释开头的行

sed -i "/^\s*#/d" `grep -rl '^\s*#' .`

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *