基本概念 在 Linux 中,cut 常用于修剪出指定位置的数据,cut 是按行为单位进行裁剪。它有三个裁剪模式:
按字节裁剪 (-b):按照字节位置裁剪
按字符裁剪 (-c):按照字符位置裁剪
按字段裁剪 (-f):按照分隔符分隔的字段裁剪
graph TB
A[输入文本] --> B[cut 命令]
B --> C[按字节 -b]
B --> D[按字符 -c]
B --> E[按字段 -f]
C --> F[输出结果]
D --> F
E --> F
style A fill:#ffcccc
style B fill:#ccffcc
style F fill:#ccccff
命令语法 基本语法
完整帮助信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 用法:cut [选项]... [文件]... -b, --bytes=列表 只选中指定的这些字节 -c, --characters=列表 只选中指定的这些字符 -d, --delimiter=分界符 使用指定分界符代替制表符作为区域分界 -f, --fields=LIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified -n with -b: don't split multibyte characters --complement 补全选中的字节、字符或域 -s, --only-delimited 不打印没有包含分界符的行 --output-delimiter=字符串 使用指定的字符串作为输出分界符,默认采用输入 的分界符 --help 显示此帮助信息并退出 --version 显示版本信息并退出 仅使用 -b, -c 或-f 中的一个。每一个列表都是专门为一个类别作出的,或者您可以用逗号隔 开要同时显示的不同类别。您的输入顺序将作为读取顺序,每个仅能输入一次。 每种参数格式表示范围如下: N 第N 个字节、字符或域 N- 从第N 个开始到所在行结束的所有字符、字节或域 N-M 从第N 个开始到第M 个之间(包括第M 个)的所有字符、字节或域 -M 从第1 个开始到第M 个之间(包括第M 个)的所有字符、字节或域 当没有文件参数,或者文件不存在时,从标准输入读取
常用选项 核心选项
选项
长选项
说明
-b
--bytes=列表
按字节位置裁剪
-c
--characters=列表
按字符位置裁剪
-f
--fields=列表
按字段位置裁剪
-d
--delimiter=分界符
指定字段分隔符(默认为制表符)
-s
--only-delimited
只输出包含分隔符的行
--complement
反向选择(输出未选中的部分)
--output-delimiter=字符串
指定输出分隔符
-n
与 -b 一起使用,不分割多字节字符
范围表示
N:第 N 个字节、字符或字段
N-:从第 N 个开始到行尾
N-M:从第 N 个到第 M 个(包含 M)
-M:从第 1 个到第 M 个(包含 M)
N,M:第 N 个和第 M 个(用逗号分隔多个位置)
使用示例 按字节裁剪(-b) 基本用法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 echo "hello world" | cut -b 1-3echo "hello world" | cut -b 1,5echo "hello world" | cut -b 5-echo "hello world" | cut -b -5
多字节字符处理 1 2 3 4 5 6 7 echo "你好:哈哈" | cut -b 1-3echo "你好:哈哈" | cut -nb 1-3
按字符裁剪(-c) 基本用法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 echo "你好:哈哈" | cut -c 1-2echo "hello" | cut -c 1,3echo "hello world" | cut -c 3-echo "hello world" | cut -c -5
处理多字节字符 1 2 3 4 5 6 echo "你好世界" | cut -c 1-2echo "你好世界" | cut -c 3-4
按字段裁剪(-f) 基本用法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 echo "你好:哈哈:hello" | cut -d: -f 1echo "你好:哈哈:hello" | cut -d: -f 1,2echo "你好:哈哈:hello" | cut -d: -f 1-2echo "你好:哈哈:hello" | cut -d: -f 2-echo "你好:哈哈:hello" | cut -d: -f -2
使用不同分隔符 1 2 3 4 5 6 7 8 9 10 11 echo "apple banana cherry" | cut -d' ' -f 1echo "name,age,city" | cut -d',' -f 2echo -e "col1\tcol2\tcol3" | cut -f 1
只输出包含分隔符的行(-s) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 cat > test.txt << EOF line1:field1:field2 line2 line3:field1:field2 EOF cut -d: -f 1 test.txtcut -d: -f 1 -s test.txt
反向选择(–complement) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 echo "12 34 56 67" | cut -b 1,4echo "12 34 56 67" | cut -b 1,4 --complementecho "12 34 56 67" | cut -b 1-4 --complementecho "a:b:c:d" | cut -d: -f 2 --complement
自定义输出分隔符(–output-delimiter) 1 2 3 4 5 6 7 8 9 10 11 echo "你好:哈哈:hello" | cut -d: -f 1-2echo "你好:哈哈:hello" | cut -d: -f 1-2 --output-delimiter='!' echo "a:b:c" | cut -d: -f 1,3 --output-delimiter=' '
实际应用场景 场景 1:处理 /etc/passwd 文件 1 2 3 4 5 6 7 8 9 10 11 cut -d: -f 1 /etc/passwdcut -d: -f 3 /etc/passwdcut -d: -f 1,3 /etc/passwdcut -d: -f 6 /etc/passwd
场景 2:处理 CSV 文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 cat > data.csv << EOF name,age,city,email Alice,25,Beijing,alice@example.com Bob,30,Shanghai,bob@example.com Charlie,28,Guangzhou,charlie@example.com EOF cut -d',' -f 1,3 data.csvcut -d',' -f 4 data.csv
场景 3:处理日志文件 1 2 3 4 5 6 cut -c 1-19 access.logcut -c 20-24 access.log
场景 4:处理系统命令输出 1 2 3 4 5 6 7 8 ifconfig eth0 | grep "inet " | cut -d' ' -f 10 ps aux | grep nginx | cut -d' ' -f 2 df -h | cut -d' ' -f 5
场景 5:处理环境变量 1 2 3 4 5 echo $PATH | cut -d: -f 1echo $PATH | cut -d: -f $(echo $PATH | tr ':' '\n' | wc -l)
场景 6:处理配置文件 1 2 3 4 5 grep -v '^#' config.conf | cut -d'=' -f 1 grep -v '^#' config.conf | cut -d'=' -f 2
与其他命令的配合 与 grep 配合 1 2 3 4 5 grep "ERROR" log.txt | cut -d' ' -f 1-2 grep "user=" access.log | cut -d'=' -f 2
与 sort 配合 1 2 3 4 5 cut -d: -f 3 /etc/passwd | sort -ncut -d',' -f 2 data.csv | sort -n
与 awk 对比 1 2 3 4 5 6 7 8 9 10 echo "a:b:c" | cut -d: -f 2echo "a:b:c" | awk -F: '{print $2}'
与 sed 配合 1 2 3 4 5 6 echo "name=Alice,age=25" | sed 's/,/:/g' | cut -d: -f 2cut -d: -f 1 /etc/passwd | sed 's/^/User: /'
管道组合 1 2 3 4 5 6 cat log.txt | \ grep "ERROR" | \ cut -d' ' -f 1,2,5 | \ sort | \ uniq
注意事项 1. 字节 vs 字符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 echo "hello" | cut -b 1-3echo "hello" | cut -c 1-3echo "你好" | cut -b 1-3echo "你好" | cut -c 1-2
2. 分隔符处理 1 2 3 4 5 6 7 echo "no delimiter" | cut -d: -f 1echo -e "has:delimiter\nno delimiter" | cut -d: -f 1 -s
3. 多个分隔符 1 2 3 4 5 6 echo "a,b;c" | awk -F'[,;]' '{print $2}'
4. 字段顺序 1 2 3 4 5 echo "a:b:c:d" | cut -d: -f 3,1
5. 空字段处理 1 2 3 4 5 6 7 echo "a::c" | cut -d: -f 2echo "a::c" | cut -d: -f 1,2,3
最佳实践 1. 选择合适的选项 1 2 3 4 5 6 7 8 9 10 11 cut -d',' -f 1,3 data.csvcut -c 1-10 log.txtecho "你好" | cut -b 1-2 echo "你好" | cut -c 1-2
2. 处理 CSV 文件 1 2 3 4 5 6 7 8 cut -d',' -f 1,2 simple.csvcsvcut -c 1,2 complex.csv
3. 性能考虑 1 2 3 4 5 6 7 8 cut -d: -f 1 large_file.txtawk -F: '{if($3>1000) print $1}' large_file.txt
4. 错误处理 1 2 3 4 5 6 7 8 9 10 11 12 13 if [ -f data.txt ]; then cut -d: -f 1 data.txt else echo "File not found" fi if [ -s data.txt ]; then cut -d: -f 1 data.txt else echo "File is empty" fi
5. 与其他工具结合 1 2 3 4 5 6 7 8 cut -d: -f 1 /etc/passwdawk -F: '{if($3>1000) print $1":"$3}' /etc/passwd grep "ERROR" log.txt | cut -d' ' -f 1-2
6. 实际应用示例 提取系统信息 1 2 3 4 5 6 7 8 cut -d: -f 3 /etc/passwd | sort -ncut -d: -f 1 /etc/groupdf -h | tail -n +2 | cut -d' ' -f 6
处理日志 1 2 3 4 5 cut -d' ' -f 1 access.log | sort | uniq -c | sort -rngrep "ERROR" error.log | cut -d' ' -f 1-2
数据处理 1 2 3 4 5 6 7 cut -d',' -f 1,3,5 data.csv > output.csvecho "/path/to/file.txt" | cut -d'/' -f $(echo "/path/to/file.txt" | tr '/' '\n' | wc -l)basename "/path/to/file.txt"
总结 cut 命令是 Linux 中一个简单而强大的文本处理工具:
核心特点
简单高效 :语法简单,处理速度快
按行处理 :逐行处理文本数据
三种模式 :字节、字符、字段三种裁剪方式
流式处理 :适合处理大文件
适用场景
✅ 提取结构化数据的特定字段
✅ 处理固定格式的文本
✅ 快速提取简单字段
✅ 与其他命令配合使用
不适用场景
❌ 复杂的文本处理(使用 awk)
❌ 需要条件判断(使用 awk)
❌ 需要计算(使用 awk)
❌ 多个分隔符(使用 awk 或 sed)
选择建议
简单字段提取 :使用 cut
复杂文本处理 :使用 awk
模式匹配和替换 :使用 sed 或 grep
多工具组合 :根据需求组合使用
掌握 cut 命令能够提高文本处理的效率,是 Linux 系统管理和数据处理的重要工具。
参考文献