Skip to content

Latest commit

 

History

History
94 lines (67 loc) · 1.83 KB

Linux_Shell文本处理.md

File metadata and controls

94 lines (67 loc) · 1.83 KB

原文链接

本文档只记录了常用的,其他的没有使用过的没有记录,可以参考原文。

Shell处理文本的工具

find

  1. 查找 txt 和 pdf文件
find . ( -name "*.txt" -o -name "*.pdf") - print
  1. 正则表达式查找 txt pdf
find . -regex ".*(.txt|.pdf)$" # -iregex 忽略大小写
  1. 否定参数

查找所有非txt文本

find . ! -name "*.txt" -print
  1. 按类型搜索:
find . -type d -print # 只列出目录
# -type f 文件 / l 符号链接

按照时间搜索

-atime 访问时间
-mtime 修改时间
-ctime 创建时间

最近7天被访问过的所有文件

find . -atime 7 -type f -print

grep

grep match_patten file # 默认访问匹配行

常用参数:

-v 输出没有匹配的文本行

-c 统计文件中包含文本的次数

grep -c "text" filename

-n 打印匹配的行号

-i 搜索时候忽略大小写

wc 统计行与字符的工具

wc -l file # 统计行数
wc -w file # 统计单词数
wc -c file # 统计字符数

awk 数据流处理工具

https://mp.weixin.qq.com/s/CtNeak1jbSW1UmyKwRm7Fg

以下shell来自【鸟哥的Linux私房菜】 P364

awk 是一个非常棒的数据处理工具,将一行分成数个字段来处理,因此,awk相当适合处理小型的数据。

模式如下

awk '条件类型1 {动作1} 条件类型2 {动作2} ...' filename

#以"|"分割数据,输出1,2,3,4行数据

awk -F"|" '{print $1,$2,$3,$4}' test.txt