Skip to content

文本处理

find - 文本搜索

基本用法

cmd
find "text" file.txt               # 搜索文本
find "error" *.log                 # 搜索多个文件
find /i "text" file.txt            # 忽略大小写
find /c "text" file.txt            # 统计匹配行数
find /n "text" file.txt            # 显示行号
find /v "text" file.txt            # 显示不匹配的行

输出说明

---------- FILE.TXT
[行号] 匹配的行内容

组合使用

cmd
type file.txt | find "error"       # 管道搜索
dir /s /b | find ".txt"            # 在目录列表中搜索
netstat -an | find "LISTENING"     # 查找监听端口
ipconfig | find "IPv4"             # 查找 IP 地址

findstr - 高级文本搜索

基本用法

cmd
findstr "pattern" file.txt         # 搜索模式
findstr /i "pattern" file.txt      # 忽略大小写
findstr /n "pattern" file.txt      # 显示行号
findstr /s "pattern" *.txt         # 递归搜索
findstr /c:"exact phrase" file.txt # 精确匹配短语

正则表达式

cmd
findstr /r "^[0-9]" file.txt       # 以数字开头
findstr /r "[a-z]" file.txt        # 包含小写字母
findstr /r "error.*failed" file.txt # error 后跟 failed
findstr /r "^$" file.txt           # 空行
findstr /r "\<word\>" file.txt     # 匹配整个单词
findstr /r "cat|dog" file.txt      # 匹配 cat 或 dog
findstr /r "[0-9][0-9][0-9]" file.txt  # 三个连续数字

正则表达式元字符

字符说明
.任意字符
*前面的字符零次或多次
+前面的字符一次或多次
^行首
$行尾
[abc]字符集
[^abc]不在字符集中
<单词开头
>单词结尾
|

多文件搜索

cmd
findstr /s /i "error" *.log        # 递归搜索日志
findstr /m "pattern" *.txt         # 只显示匹配的文件名
findstr /g:pattern.txt file.txt    # 从文件读取模式
findstr /f:files.txt "pattern"     # 从文件读取文件列表

输出控制

cmd
findstr /o "pattern" file.txt      # 显示字符偏移
findstr /a:0A "pattern" file.txt   # 设置颜色高亮
findstr /b "pattern" file.txt      # 匹配行首
findstr /e "pattern" file.txt      # 匹配行尾

type - 显示文件内容

基本用法

cmd
type file.txt                      # 显示文件内容
type file1.txt file2.txt           # 显示多个文件
type file.txt | more               # 分页显示
type file.txt > output.txt         # 输出到文件
type file.txt | find "pattern"     # 结合 find 使用

合并文件

cmd
type file1.txt file2.txt > merged.txt  # 合并文件
type *.txt > all.txt                   # 合并所有 txt 文件

more - 分页显示

基本用法

cmd
more file.txt                      # 分页显示
type file.txt | more               # 管道分页
more +10 file.txt                  # 从第10行开始
more /s file.txt                   # 压缩空行
more /c file.txt                   # 清屏后显示
more /p file.txt                   # 扩展换页符

交互命令

命令功能
空格下一页
Enter下一行
q退出
/pattern搜索
n下一个匹配
=显示行号

sort - 排序

基本用法

cmd
sort file.txt                      # 排序文件
sort /r file.txt                   # 反向排序
sort /+2 file.txt                  # 从第2列开始排序
sort file.txt /o output.txt        # 输出到文件
type file.txt | sort               # 管道排序

排序选项

cmd
sort /+n file.txt                  # 从第 n 列开始
sort /r file.txt                   # 降序排序
sort /unique file.txt              # 去重排序
sort /l file.txt                   # 区分大小写
sort /l "C" file.txt               # 指定区域设置

文本替换

使用变量替换

cmd
set str=Hello World
echo %str:World=Windows%           # 替换: Hello Windows
echo %str:o=0%                     # 替换: Hell0 W0rld

使用 PowerShell 进行替换

cmd
powershell -Command "(Get-Content file.txt) -replace 'old', 'new' | Set-Content file.txt"
powershell -Command "(Get-Content file.txt) -replace '[0-9]+', 'NUM' | Set-Content output.txt"

文本提取

使用 for 循环提取

cmd
for /f "tokens=1,2" %a in (file.txt) do @echo %a %b
for /f "tokens=1-3" %a in (file.txt) do @echo %a %b %c
for /f "tokens=2 delims=," %a in (file.txt) do @echo %a
for /f "skip=1" %a in (file.txt) do @echo %a    # 跳过第一行

for /f 选项

选项说明
tokens提取的列
delims分隔符
skip跳过行数
eol注释字符
usebackq使用反引号

从命令输出提取

cmd
for /f "tokens=2" %a in ('netstat -an ^| findstr LISTENING') do @echo %a
for /f "tokens=*" %a in ('dir /b') do @echo %a
for /f "usebackq tokens=1" %a in (`type file.txt`) do @echo %a

文本处理技巧

提取文件名和扩展名

cmd
for %f in (*.txt) do echo %~nf     # 文件名(无扩展名)
for %f in (*.txt) do echo %~xf     # 扩展名
for %f in (*.txt) do echo %~dpnf   # 完整路径(无扩展名)

处理 CSV 文件

cmd
for /f "tokens=1,2,3 delims=," %a in (data.csv) do (
    echo Column1: %a, Column2: %b, Column3: %c
)

统计文件行数

cmd
find /c /v "" file.txt             # 统计行数
for /f %a in ('find /c /v "" file.txt') do set lines=%a

提取特定行

cmd
for /f "skip=5 tokens=*" %a in (file.txt) do @echo %a & goto :done
:done

PowerShell 文本处理

Select-String

powershell
Select-String -Path file.txt -Pattern "error"
Select-String -Path file.txt -Pattern "error" -CaseSensitive
Select-String -Path *.log -Pattern "error|warning"
Select-String -Path file.txt -Pattern "\d+" -AllMatches

Get-Content

powershell
Get-Content file.txt               # 读取文件
Get-Content file.txt -TotalCount 10  # 读取前10行
Get-Content file.txt -Tail 10      # 读取后10行
Get-Content file.txt -Wait         # 实时监控
(Get-Content file.txt)[0..9]       # 读取前10行

字符串操作

powershell
"text".Replace("old", "new")       # 替换
"text".Substring(0, 3)             # 截取
"text".Split(",")                  # 分割
"text".ToUpper()                   # 大写
"text".ToLower()                   # 小写
"text".Trim()                      # 去除首尾空格
-text".TrimStart()                 # 去除开头空格
"text".TrimEnd()                   # 去除结尾空格

正则表达式

powershell
"text" -match "pattern"            # 匹配
"text" -replace "pattern", "new"   # 替换
"text" -split "pattern"            # 分割
[regex]::Matches("text", "pattern") # 所有匹配

实用示例

日志分析

cmd
findstr /i "error" app.log | find /c "error"    # 统计错误数
findstr /i "error" app.log | findstr /i "database"  # 数据库错误
type app.log | findstr /i "2023-12-25" | findstr /i "error"

提取 IP 地址

cmd
for /f "tokens=2 delims=:" %a in ('ipconfig ^| findstr "IPv4"') do @echo %a

处理配置文件

cmd
for /f "tokens=1,2 delims==" %a in (config.ini) do (
    if "%a"=="server" echo Server: %b
)

批量替换文件内容

cmd
for %f in (*.txt) do (
    powershell -Command "(Get-Content '%f') -replace 'old', 'new' | Set-Content '%f'"
)

下一步学习