linux中cut命令的用法

时间:2025-04-06 07:36:18

名称:cut 

使用权限:所有使用者 

用法:cut -cnum1-num2 filename 

说明:显示每行从开头算起 num1 到 num2 的文字。 

范例: 

shell>> cat example 
test2 
this is test1 
shell>> cut -c1-6 example ## print 开头算起前 6 个字元 
test2 
this i 

-c m-n 表示显示每一行的第m个字元到第n个字元。例如: 

---------file----------- 
liubi 23 14000 
---------file----------- 
# cut -c 1-5,10-14 file 
liubi 14000 

-f m-n 表示显示第m栏到第n栏(使用tab分隔)。例如: 
---------file----------- 
liubi 23 14000 
---------file----------- 
# cut -f 1,3 file 
liubi 14000 

-c 和 -f 参数可以跟以下子参数:
m 第m个字符或字段
m- 从第m个字符或字段到文件结束
m-n 从第m个到第n个字符或字段
-n 从第1个到第n个字符或字段

我们经常会遇到需要取出分字段的文件的某些特定字段,例如 /etc/password就是通过":"分隔各个字段的。可以通过cut命令来实现。例如,我们希望将系统账号名保存到特定的文件,就可以:
cut -d: -f 1 /etc/passwd > /tmp/users
-d用来定义分隔符,默认为tab键,-f表示需要取得哪个字段
如:
使用|分隔 
cut -d'|' -f2 > 
使用:分隔 
cut -d':' -f2 >
这里使用单引号或双引号皆可。