linux shell 笔记

sed编辑器基础应用,可用类似path/*.txt的格式,同时操作多个文件。

sed -i 's/abc/AA&BB/g' test.txt # 替换文件中abc为AAabcBB,其中&代表被替换字符串
sed -i 's/^/head/g' test.txt # 在文件每行开头添加head,^代表行首,且替换后字符串中无需用&再次代表
sed -i 's/$/tail/g' test.txt # 在文件每行结尾添加tail,$代表行尾,且替换后字符串中无需用&再次代表
sed -i 's/old_string/new_string/g' ./*.txt # 替换当前文件夹下所有*.txt文件中old_string为new_string

# Example
sed -i s/" </name"/"</name"/g path_to_ann/*.xml

查找当前目录下,包含substring的文件

grep -rn "substring" *

 查找当前目录下,文件名符合*.txt且包含字符串substring的文件

grep 'substring' -r --include=*.txt ./
原文地址:https://www.cnblogs.com/xbit/p/9261841.html