文章目录
Notice:有的docker中可能没装vim/vi,但你又希望修改文件(修改处不多),可以使用
sed
临时救命
sed
- stream editor for filtering and transforming text
本质是个 编辑器!用于文本的增删改查
e.g. 打印一个文件的10 到20 行
sed -n '10,20'p b.txt
- -n, --quiet, --silent
suppress automatic printing of pattern space (取消了默认打印) - -p, Print the current pattern space.
e.g. 将末行输出
sed -n '$'p b.txt
- ‘$’ ,means the end line
e.g.只打印第10行
sed -n '10'p b.txt
e.g. 文本输出替换 【这个最实用】
sed "s#for#just for#g" a.txt
在输出 a.txt 的内容时,将其中的for 替换成 just for ,但不改变原来文本的内容 【相当于 dry-run
】
sed -i "s#steel#iron steel #g" a.txt
直接将文本中的内容steel 替换成 iron steel
- s: 正则表达式开始(通俗说就是 查找并替换)
s/regexp/replacement/
Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp. - g :全局 global
- -i : edit files in place
- sg: 全局查找并替换