跳到主要内容

Linux 文本处理类命令

head, tail

grep

  • -v
  • -n 显示行号
# 排除空行和注释
grep -v '^$' filepath | grep -Ev '^\s*#'

从指定文件中搜索

find .obsidian -name "data.json" -exec grep -l "Inbox\|Archives" {} \;
grep -rn --include="data.json" "Inbox\|Archives" .obsidian
rg "Inbox|Archives" -g "**/data.json" .obsidian

sed

# 删除行
sed '1d;$d'

# 替换换行
sed ':a;N;$!ba;s/\n/,/g' path/file

# 删除行首空格
sed 's/^[ ]*//g' path/file

awk

cut

tr

# 替换换行
tr "\n" "," < path/file > path/to/file

sort

uniq

comm

comm -3 <(command1 | sort) <(command2 | sort)

join

paste

diff

# 比较两个命令的输出
diff <(command1) <(command2)