文件操作
grep
- 基本查找
grep “xxx” text.txt- 递归查找
grep -r “xxx” text.txt- 查找结果显示行号
grep -n “xxx” text.txt- 指定查找结果行数
grep “xxx” text.txt -m 1- 正则查找
grep -P “regex” text.txt- 只显示命中内容
grep “xxx” text.txt -o
find
- 文件名模糊匹配查找
find ./ -name "*.cpp"
sed
- 按行号截取文件内容
sed -n ‘1, 100p’ text.txt- 替换文本内容
sed -i ‘s/查找字段/替换字段/g’ text.txt
xxx|sed -e ‘s/查找字段/替换字段/g’ 【命令行模式】
cat
- 从头至尾输出文件
cat text.txt
tac
- 从尾至头输出文件
tac text.txt
tail
- 终端不间断输出文件尾
tail -f text.txt
wc
- 输出文本行数
wc -l text.txt- 输出文本字数
wc -m text.txt
sort
- 文本排序
sort text.txt
cut
- 分割文本
cut -f1 -d:
其中-f表示展示哪个域,-d表示按什么字符分割,常配合grep查找目标匹配串行号
例如:grep -n ‘xxx’ text.txt | cut -f1 -d: