cut用法

cut用来从标准输入或者文本文件中剪切列或者域。

其使用方法为:

cut [options] file1 file2 

其可用的选项有:

 -c list 指定剪切字符数。 

 -f field 指定剪切域数。 

 -d 指定与空格和t a b键不同的域分隔符。 

 - c用来指定剪切范围,如下所示:

      - c 1,5-7 剪切第1个字符,然后是第5到第7个字符。 

    -c1-50 剪切前5 0个字符。 

 -f 格式与- c相同。 

    -f 1 ,5 剪切 第1域,第5域。 

   - f 1,10-12 剪切第1域,第1 0域到第1 2域。 

具体示例如下:

cat 1.txt 

hello1 hello11

hello2 hello12

hello3 hello13

获取每行第一个字符:cut -c 1 1.txt 
h
h
h
当修改将起始改为空格时:cat 1.txt 
 hello1 hello11
hello2 hello12
hello3 hello13
执行上面的命令:cut -c 1 1.txt 
 
h
h
由此可以看出其就是取第一个字符,不论是不是空格
获取多个字符:cut -c 1-4 1.txt 
hell
hell
hell
 
获取第一个域:cut -f 1 1.txt 
hello1 hello11
hello2 hello12
hello3 hello13
指定分隔符为空格:cut -d " " -f 1 1.txt 
hello1
hello2
hello3
 
cut -d " " -f 1,2 1.txt 
hello1 hello11
hello2 hello12
hello3 hello13
 
cut -d " " -f 1-2 1.txt 
hello1 hello11
hello2 hello12
hello3 hello13
 
cut -dl -f 2 1.txt 
 
 
 
 结果为空
cut -d "l" -f 1-2 1.txt 
hel
hel
hel
看出其多个域时会包括分隔符。
awk -F"l" '{print $1,$2}' 1.txt 
he 
he 
he 
其使用l作为了分隔符,即使多个域,其的结果也不会包含分隔符。
 
 
 
 
 

转载于:https://www.cnblogs.com/lovemdx/archive/2012/04/19/2457352.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值