Linux命令-spell(Unix 拼写检查工具 —— 文本校对的基础利器)

Linux命令-spell(Unix 拼写检查工具 ------ 文本校对的基础利器)

    • [🔰 简介](#🔰 简介)
    • [📖 语法](#📖 语法)
    • [⚙️ 选项](#⚙️ 选项)
    • [💡 示例](#💡 示例)
      • [示例 1:基本拼写检查](#示例 1:基本拼写检查)
      • [示例 2:管道模式输入](#示例 2:管道模式输入)
      • [示例 3:使用不同字典](#示例 3:使用不同字典)
      • [示例 4:排除已知的专有名词](#示例 4:排除已知的专有名词)
      • [示例 5:在现代系统中使用 hunspell/aspell](#示例 5:在现代系统中使用 hunspell/aspell)
      • [示例 6:批量检查多个文件](#示例 6:批量检查多个文件)
      • [示例 7:集成到 Git 工作流](#示例 7:集成到 Git 工作流)
    • [⚠️ 注意](#⚠️ 注意)
      • [spell vs hunspell vs aspell 对比](#spell vs hunspell vs aspell 对比)
      • 安装其他语言字典
      • [spell 的局限性](#spell 的局限性)
    • [📝 总结](#📝 总结)
    • [📚 相关命令](#📚 相关命令)

快速参考spell 是 Unix/Linux 系统中的传统英文拼写检查工具。它从标准输入或文件中读取文本,逐一检查每个单词是否在系统字典中存在,输出所有拼写可疑的单词。spellispell/hunspell 系列工具的底层接口,现代 Linux 发行版中通常由 aspellhunspell 提供兼容实现。


🔰 简介

spell 诞生于 Unix 早期,是最早的自动化文本校对工具之一。它的工作原理非常简单:将输入文本中的单词与系统字典对比,不在字典中的单词即被视为"拼写错误"并输出。

在现代 Linux 发行版中,原始的 spell 命令通常不存在于默认安装中,需要通过安装 aspellhunspell 包来获得兼容的 spell 实现。

安装位置

bash 复制代码
# 查看 spell 命令路径
which spell
# 输出:/usr/bin/spell  或  spell 未安装

# 查看 spell 的来源
# CentOS/RHEL 7
rpm -qf /usr/bin/spell 2>/dev/null
# 输出:aspell-0.60.6.1-9.el7.x86_64

# CentOS/RHEL 8+
rpm -qf /usr/bin/spell 2>/dev/null
# 输出:hunspell-1.7.0-2.el8.x86_64

# Debian/Ubuntu
dpkg -S /usr/bin/spell 2>/dev/null
# 输出:aspell: /usr/bin/spell

# 安装
# CentOS/RHEL
sudo yum install aspell aspell-en
# 或
sudo yum install hunspell hunspell-en

# Debian/Ubuntu
sudo apt install aspell aspell-en
# 或
sudo apt install hunspell hunspell-en-us

📖 语法

复制代码
spell [选项] [文件...]

如果不指定文件,从标准输入读取。


⚙️ 选项

选项 说明
-b 使用英式英语(British English)字典
-d 字典 指定要使用的字典文件
-v 详细模式,输出不在字典中的单词并附带可能的正确拼写
-x 同时输出每个单词的行号
-l ispell -l 管道模式,输出错误单词列表
-a 管道模式(与 ispell/aspell 兼容的交互模式)
-i 忽略已知的错误单词列表(需配合个人字典)
--encoding=编码 指定输入文本的字符编码

💡 示例

示例 1:基本拼写检查

bash 复制代码
# 创建一个包含拼写错误的测试文件
cat > /tmp/test_doc.txt << 'EOF'
This is a test dokcument with som mistaks.
The quick brown fox jumps over the laizy dog.
I want to lern Linux kommands and shell skripting.
EOF

# 运行 spell 检查
spell /tmp/test_doc.txt
# 输出:
# dokcument
# kommands
# laizy
# lern
# mistaks
# skripting
# som

# 每个输出的单词都是字典中不存在的

示例 2:管道模式输入

bash 复制代码
# 从管道读取文本
echo "The sysadmin must konfigure the server propperly" | spell
# 输出:
# konfigure
# propperly
# sysadmin

# 检查命令输出的文本
man ls 2>/dev/null | col -b | spell | sort -u | head -10
# 输出(man 手册中的专有名词会被标记):
# alloc
# cmd
# COLOR
# dir
# FILE
# gid
# ...

# 检查 Shell 脚本中的注释
grep '^#' /etc/profile | spell | sort -u
# 输出(发行版相关的专有名词会被列出)

示例 3:使用不同字典

bash 复制代码
# 使用英式英语字典
echo "The colour of the centre is gray" | spell -b
# 输出:gray
# (英式英语中 "colour" 和 "centre" 是正确的,"gray" 被认为是美式拼写)

# 使用美式英语字典(默认)
echo "The colour of the centre is gray" | spell
# 输出:
# centre
# colour
# (美式英语中应写 "color" 和 "center")

# 指定自定义字典
spell -d /usr/share/dict/words /tmp/test_doc.txt
# 字典文件路径因发行版不同而异:
# CentOS/RHEL: /usr/share/dict/words (需安装 words 包)
# Debian/Ubuntu: /usr/share/dict/american-english
#                /usr/share/dict/british-english

示例 4:排除已知的专有名词

bash 复制代码
# 创建个人字典文件
cat > /tmp/my_dict.txt << 'EOF'
sysadmin
nginx
kubernetes
EOF

# 使用 ispell 管道模式(现代替代)
echo "The sysadmin deployed nginx to kubernetes" | \
  hunspell -l -p /tmp/my_dict.txt
# 输出:(空 ------ 所有词都在字典或自定义词表中)
# 无输出表示拼写全部正确

# 不使用自定义字典的对比
echo "The sysadmin deployed nginx to kubernetes" | \
  hunspell -l
# 输出:
# kubernetes
# nginx
# sysadmin

# 使用 aspell 的等价命令
echo "The sysadmin deployed nginx to kubernetes" | \
  aspell list --personal=/tmp/my_dict.txt

示例 5:在现代系统中使用 hunspell/aspell

bash 复制代码
# === hunspell 方式(推荐!)===

# 交互式拼写检查
hunspell /tmp/test_doc.txt
# 输出:
# dokcument
# 0: document [dokcument]
# suggestions: document
# Replace with: document
# (提供交互式替换建议)

# 仅列出错误单词(等价于 spell)
hunspell -l /tmp/test_doc.txt
# 输出:
# dokcument
# kommands
# laizy
# lern
# mistaks
# skripting
# som

# === aspell 方式 ===

# 仅列出错误单词
aspell list < /tmp/test_doc.txt
# 输出:
# dokcument
# kommands
# laizy
# lern
# mistaks
# skripting
# som

# 交互式检查(有替换建议)
aspell check /tmp/test_doc.txt
# 进入全屏交互界面,逐个单词提供修正建议

# 管道模式获取建议
echo "kommands" | aspell -a
# 输出:
# @(#) International Ispell Version 3.1.20
# & kommands 4 0: commands, command's, command, commanders

示例 6:批量检查多个文件

bash 复制代码
# 批量检查目录下所有文本文件的拼写
for file in /tmp/docs/*.txt; do
    echo "=== Checking: $file ==="
    hunspell -l "$file" | sort -u
    echo ""
done
# 输出:
# === Checking: /tmp/docs/chapter1.txt ===
# impliment
# resieve
# targit
#
# === Checking: /tmp/docs/chapter2.txt ===
# comunication
# wirkflow

# 统计整个项目的拼写错误
hunspell -l /tmp/docs/*.txt | sort | uniq -c | sort -rn | head -10
# 输出:
#      15 konfig
#      12 impliment
#       8 wirkflow
#       5 comunication
#       3 resieve
#       2 targit

示例 7:集成到 Git 工作流

bash 复制代码
# Git pre-commit hook:检查文档拼写
cat > /tmp/spell-check-hook.sh << 'HOOK'
#!/bin/bash
# 将此脚本保存为 .git/hooks/pre-commit

errors=0
for file in $(git diff --cached --name-only --diff-filter=ACM | grep '\.\(md\|txt\|rst\)$'); do
    if [ -f "$file" ]; then
        misspelled=$(hunspell -l "$file" 2>/dev/null)
        if [ -n "$misspelled" ]; then
            echo "=== 拼写问题: $file ==="
            echo "$misspelled" | sort -u
            echo ""
            errors=$((errors + 1))
        fi
    fi
done

if [ $errors -gt 0 ]; then
    echo "发现 $errors 个文件存在拼写问题"
    echo "如需忽略某些词,将它们添加到 .spell-ignore 文件"
    exit 1
fi
HOOK

chmod +x /tmp/spell-check-hook.sh

⚠️ 注意

spell vs hunspell vs aspell 对比

bash 复制代码
# === spell(传统 Unix 命令)===
# 仅输出错误单词,无交互、无建议
spell file.txt
# 输出:简单的单词列表

# === hunspell(现代推荐)===
# - LibreOffice/OpenOffice 使用
# - Firefox/Chrome 使用
# - 功能丰富,支持多语言
# - 词干分析、复合词支持
hunspell -l file.txt           # 等价于 spell
hunspell file.txt              # 交互模式(有建议)
hunspell -d en_US file.txt     # 指定字典

# === aspell(传统替代)===
# - GNU 项目出品
# - 比 spell 更好的建议
# - 支持个人字典
aspell list < file.txt         # 等价于 spell
aspell check file.txt          # 全屏交互模式
aspell -a                      # 管道模式的拼写服务器

# 功能对比表:
# ┌──────────┬────────┬───────────┬──────────┐
# │ 功能     │ spell  │ hunspell  │ aspell   │
# ├──────────┼────────┼───────────┼──────────┤
# │ 拼写检查 │ ✅     │ ✅        │ ✅       │
# │ 修正建议 │ ❌     │ ✅        │ ✅       │
# │ 多语言   │ 有限   │ ✅        │ ✅       │
# │ 交互模式 │ ❌     │ ✅        │ ✅       │
# │ 个人字典 │ ❌     │ ✅        │ ✅       │
# │ 性能     │ ✅     │ ✅        │ 较慢     │
# └──────────┴────────┴───────────┴──────────┘

安装其他语言字典

bash 复制代码
# CentOS/RHEL
sudo yum install hunspell-fr      # 法语
sudo yum install hunspell-de      # 德语
sudo yum install hunspell-es      # 西班牙语

# Debian/Ubuntu
sudo apt install hunspell-fr
sudo apt install hunspell-de-de
sudo apt install myspell-es

# 查看已安装的字典
hunspell -D
# 输出:
# SEARCH PATH:
# .::/usr/share/hunspell:/usr/share/myspell:
# /usr/share/myspell/dicts:/Library/Spelling
# AVAILABLE DICTIONARIES (path is not mandatory for -d option):
# /usr/share/hunspell/en_US
# /usr/share/hunspell/en_GB

spell 的局限性

bash 复制代码
# spell 无法识别上下文相关的错误
echo "Their going to there house over their" | spell
# 输出:(空)
# "Their" 和 "there" 都是正确拼写的单词,但用法完全错误!
# 正确应为:"They're going to their house over there"

# spell 无法处理专有名词
echo "I deployed the app using Kubernetes and Terraform" | spell
# 输出:
# Kubernetes
# Terraform
# (专有名词被标记为错误,但实际是正确的)

# spell 无法处理技术术语
echo "The API endpoint returns JSON data via HTTP" | spell
# 输出:
# API
# JSON
# HTTP
# (技术缩写被标记为错误)

📝 总结

spell 是 Linux 文本校对工具链的起点:

  • 核心功能:从文本中找出不在系统字典中的单词
  • 现代替代hunspell -laspell list 提供等价功能,且增加了 交互修正、拼写建议、多语言支持
  • 最佳实践:配合个人字典文件排除专有名词和技术术语
  • 局限性 :不检查语法和上下文(如 their/there/they're),仅检查单词级拼写
  • 应用场景:文档校对、CI/CD 拼写检查、代码注释审阅

📚 相关命令

命令 说明
hunspell 推荐 现代拼写检查工具(LibreOffice/Firefox 使用)
aspell GNU 拼写检查器,功能比 spell 更丰富
ispell 早期的交互式拼写检查器(hunspell 的前身)
look 在字典中查找以指定字符串开头的单词
wc 统计文本的行数、单词数、字符数
grep 文本搜索(可用于查找特定单词的使用情况)
相关推荐
考虑考虑5 小时前
docker compose V2版本新属性
运维·后端·自动化运维
AlanBruce5 小时前
摩尔信使MThings功能综述与应用使用指南
linux·自动化·plc·mthings·摩尔信使
小小的木头人6 小时前
Ubuntu Samba修改端口绕过445封禁挂载
运维·ubuntu
wdfk_prog8 小时前
ROS教程:01 搭建 ROS1 Noetic Docker 开发环境
运维·缓存·docker·容器·ros
SEO_juper8 小时前
Java 并发编程实战:从线程基础到高并发架构
运维·人工智能·爬虫·chatgpt·seo
szephyr8 小时前
Docker + Compose 实战:把本地项目一键搬到云服务器,附完整配置
运维·docker·容器·部署·云服务器
wdfk_prog9 小时前
ros教程02:创建 catkin Workspace、Package 与第一个 ROS1 C++ Node
运维·缓存·docker·容器·ros
亚川楼宇自控系统数据中心厂家9 小时前
高校 / 医院 / 政务云数据中心 IBMS 系统怎么落地?
运维
陈陈CHENCHEN10 小时前
【Linux】服务器根目录磁盘扩容操作记录
linux·运维·服务器
GeW11 小时前
RHCE快速拿证全攻略:考点拆解+实验强化+时间规划,一次通关
linux