Skip to content

Latest commit

 

History

History
68 lines (47 loc) · 1.12 KB

sed.md

File metadata and controls

68 lines (47 loc) · 1.12 KB

sed 使用示例

[TOC]

参考文档:https://www.cyberciti.biz/faq/how-to-use-sed-to-find-and-replace-text-in-files-in-linux-unix-shell/

1. 替换指定字符串

1.1 示例数据

$ cat sed/hello.txt
The is a test file created by nixCrft for demo purpose.
foo is good.
Foo is nice.
I love FOO.

1.2 替换命令

1.2.1 全局替换

$ sed 's/foo/bar/g' sed/hello.txt
The is a test file created by nixCrft for demo purpose.
bar is good.
Foo is nice.
I love FOO.

1.2.2 大小写不敏感替换

安装gnu sed

$ brew install gnu-sed

使用gsed执行大小些不敏感替换

$ gsed 's/foo/bar/gI' sed/hello.txt
The is a test file created by nixCrft for demo purpose.
bar is good.
bar is nice.
I love bar.

2. 文件内查找直接替换

2.1 通过-i '.bak'指定备份文件后缀

$ sed -i '.bak' 's/foo/bar/g' sed/hello.txt

2.2 通过-i ''跳过备份文件

$ sed -i '' 's/foo/bar/g' sed/hello.txt

3. 多文件查找替换 - find -exec sed

$ find sed -name *.txt -exec sed -i '' 's/foo/bar/g' {} \;