grep


选项:

  • -E 正则表达式筛选
  • -o 打印仅仅匹配的部分
  • -c 统计匹配的数量
  • -B NUM 输出选中行的前NUM行

示例:

  1. grep -Eo 'expr'
  2. grep '123' -B10 #打印包含123的前10行

awk


示例:

awk -F 'd' 'condition{opr}' < '输入'
  • -F 指定 ‘d’作为分隔符
  • 在第二个’’可使用特殊变量
    • $NF 使用awk切割的变量的数量

sed


一次从输入中读取一行数据
根据所提供的编辑器命令匹配数据
按照命令修改流中的数据
将新的数据输出到STDOUT

sed options script file

options:

-e script 处理输入时,添加script命令
-f file 处理输入时, 将file中的命令添加
-n 不产生输出时,使用print命令来完成输出

script语法:

's/123/345/' #123替换成345
'/cat/d' #删除带有cat的一行
'2,3 d' # 删除2,3行

用法:

  1. 在命令行定义编辑器命令

字符串替换功能! 默认并不会替换文件内容

echo "This is a test" | sed 's/test/big test/'
# This is a big test
  1. 在命令行使用多个编辑器命令 ```shell cat file :<<! The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog !

sed -e ‘s/brown/green/; s/dog/cat/‘ file

使用-e执行多个命令, 命令之间用; 分割

:<<! The quick green fox jumps over the lazy cat. The quick green fox jumps over the lazy cat. The quick green fox jumps over the lazy cat. The quick green fox jumps over the lazy cat. !

sed -e ‘

s/brown/green/ s/fox/elephant/ s/dog/cat/‘ file

使用提示符分割,注意单引号的位置

:<<! The quick green elephant jumps over the lazy cat. The quick green elephant jumps over the lazy cat. The quick green elephant jumps over the lazy cat. The quick green elephant jumps over the lazy cat. !


3. 从文件中读取编辑器命令
```shell
cat script.sed
:<<!
s/brown/green
s/fox/elephant/
s/dog/cat/
!

sed -f script.sed file
:<<!
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
!

nl


行号的格式规定命令

举例:

# nl -b a -n rz -w 2 test1
:<<!
nl 显示行号
-b a 空行仍然显示行号
-n rz 行号在栏位最右方显示且补0
-w 栏位占据的位数
!

jq


使用json格式输出数据
如: cat filename | jq

sort


选项:

  • -u 每一行唯一

示例:

sort -u

tac


tac倒序输出