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/p/2457352.html