grep, cut, sed, sort, awk运用

1. 基础的日志查看命令

head - Read from the top. 
head app.log #
Shows the first 10 lines by default
head -n 5 app.log # first 5 lines
tail - Read from the bottom. 
tail -400f demo.log #监控最后400行日志文件的变化 等价 tail -n 400 -f demo.log(-f参数是实时)
tail -n 5 app.log # last 5 lines

less — Scroll Up and Down

Opens the file in an interactive viewer, lets you scroll freely

less demo.log #查看日志文件,支持上下滚屏,查找功能

less -N app.log # show line numbers

more — Scroll Down Only

Older, simpler viewer. Can only scroll forward:

more app.log 

uniq -c demo.log  #标记该行重复的数量,不重复值为1

 

2. grep 命令简单使用

规则:grep [选项]...模式 [文件]...    (模式是正则表达式)

grep 'ERR' appcrawler.log  # 在文件appcrawler.log中查找所有包含ERR的行

grep -c 'ERROR' appcrawler.log #输出文件
appcrawler.log中查找所有包行ERROR的行的数量

grep -v 'ERROR'
appcrawler.log #查找不含"ERROR"的行
grep -o 'order-fix.curr_id:\([0-9]\+\)' demo.log #-o选项只提取order-fix.curr_id:xxx的内容(而不是一整行),并输出到屏幕上。 类似于order-fix.curr_id:10117

grep -l "error" *.log # 只显示含匹配内容的文件名

# 显示匹配行的上下文
grep -A 3 "error" app.log # 匹配行 + 后 3 行 (After)
grep -B 3 "error" app.log # 匹配行 + 前 3 行 (Before)
grep -C 3 "error" app.log # 匹配行 + 前后各 3 行 (Context)

grep -w "log" app.log # 精确匹配整个单词(不匹配 logger)


-i 不区分大小写
-n 显示行号
--color=auto 将关键字高亮展示
如果要设置默认高亮显示,可以在配置文件中增加 alias grep = 'grep --color=auto'

截屏2026-05-14 18.07.32

 

截屏2026-05-14 18.19.02

 

 

3. 列截取工具cut

cut 选项 文件名

-d 自定义分隔符 
-f 和-d一起使用指定截取第几咧
-c 截取第几个字符

cut -d: -f1 test.txt  # 以:分割,截取第一列内容
cut -d ' ' -f 1,4,7 test.txt. # 以空格分割,截取第1,4,7列内容
cut -c4 test.txt  # 截取每一行第4个字符
cut -c5-7 test.txt  # 截取每一行第5-7个字符
cut -c5- test.txt  # 从第五个字符开始,截取后面所有的字符

echo "HelloWorld" | cut -c 1-5. # Hello

Explanation

  • -c = characters
  • 1-5 = extract character positions 1 through 5

 

echo "apple,banana,orange" | cut -d "," -f 2  # banana

Explanation

    • -d ","
      → delimiter is comma
    • -f 2
      → select field 2

 

4. 精简日志内容 sed

 通过sed更改文本

sed '2a\

This is a new linettttt

' app.log# 在文件第二行之后追加一行文字, 只在终端展示修改后的结果,不修改原文件

or sed $'2a\\\nThis is a new linettttt' app.log

sed '1i\TITLE' test.txt     # 在文件第一行之前插入内容 TITLE

截屏2026-05-17 02.23.38

 

截屏2026-05-17 02.25.04

 

sed '2,4d' test.txt    # 删除文件第二行到第四行的内容

sed '2d' file.txt. # delete line 2

 

sed 's/apple/banana/g' test.txt    # 将文件内所有的apple都替换为banana

Without g, only first match is replaced 

# g表示全局替换 substitute old with new

 

- i :in-place editing

sed -i '' 's/ERROR/WARN/g' app.log  # 不在console输出任何内容,直接修改原文件 

5. sort 将整个文件重新按行排序

sort 选项 文件

-n 以数字排序,默认以字符排序
-u  去重
-r 降序排列,默认是升序
-t 分隔符
-k 第n列
-o 将结果输出到文件中

sort -n -t: -k3 test.txt  # 按第三列以数字升序排列
sort -nr -t: -k3 test.txt  # 按第三列以数字降序排列
sort -n test.txt -o test1.txt  # 将结果输出到新文件中

 

6. awk

$ echo 'this is a test' | awk '{print $0}'    # $0表示打印每一行内容
this is a test
>$ echo 'this is a test' | awk '{print $3}'    # 默认以空格分隔,打印第三列内容
a

-F:定义字段分隔符,默认的分隔符是空格

>$ awk -F ':' '{ print $1 }' demo.txt    # 表示以:分隔,打印第一列内容
root
daemon
bin

变量NF表示有多少个字段,$NF表示最后一个字段,${NF-1}表示倒数第二个字段
>$ echo 'this is a test' | awk '{print $NF}'
test
awk -F ':' '{print $1, $(NF-1)}' demo.txt
root /root
daemon /usr/sbin
bin /bin
 
 

 

 

 

posted on 2019-09-30 18:34  mlllily  阅读(275)  评论(0)    收藏  举报