cut Split the contents of the line in the file by the specified separator and output .
cut Commands can also be used to delete specified lines or segments in a file , Then print out the changes . It may also be used to splice file contents into a new file , Function and cat similar .
cut [ Options ] [ Link file name ] cut [OPTION] [LINKNAME]
-b # Only display the specified ( Number of bytes ) The content of -c # Only display the specified ( Number of characters ) The content of -d # Specifies the separator for the field , The default is “TAB” -f # Print the specified fields ( Column ) The content of -n # And “-b” Option combination , Don't split multibyte characters -s # Don't print the contents of lines that don't contain delimiters --help # Print help --version # Print version information
cut The description of the byte or character range specified in the command is as follows :
N # from 1 byte 、 The character or field starts at N Bytes 、 Character or field N- # From N Bytes 、 Character or field to the end of the line N-M # From N Bytes 、 Character or field to M Bytes , Character or field -M # From 1 Bytes 、 Character or field to M Bytes 、 Character or field Be careful : All range values need to be integers , Such as :10,10-,10-20,-20.
# Print a specified number of bytes of content [root@centos7 testdir]# cat mingongge1.txt 1111 11 111111 1 111 1 1 11 22222222222 222 2222 22 2 2 2 33333333333 333333 3333 333 33 444444444444 444 444444444 [root@centos7 testdir]# cut -b 3 mingongge1.txt 1 2 3 4 # Intercepts the contents of the specified field [root@centos7 testdir]# cat cuttest.txt 1 2 3 4 5 6 8 9 8 7 6 5 4 3 2 1 9 8 7 6 5 # Separated by spaces , Print the first column of each row [root@centos7 testdir]# cut -f1 -d" " cuttest.txt 1 9 2 # Separated by spaces , Print the first and third columns of each row [root@centos7 testdir]# cut -f1,3 -d" " cuttest.txt 1 3 9 7 2 9 # Separated by spaces , Print the third column of each row to the end [root@centos7 testdir]# cut -f3- -d" " cuttest.txt 3 4 5 6 8 7 6 5 4 3 9 8 7 6 5 # Intercept every line at 2-5 Characters [root@centos7 testdir]# cut -c 2-5 cuttest.txt 2 3 8 7 1 9 # Intercept the first to fifth characters of each line [root@centos7 testdir]# cut -c -5 cuttest.txt 1 2 3 9 8 7 2 1 9 # Intercept the fifth to last character of each line [root@centos7 testdir]# cut -c 5- cuttest.txt 3 4 5 6 8 7 6 5 4 3 9 8 7 6 5
Specifying the use of the separator to intercept content is very practical , It's often used at work .
This article is from WeChat official account. - The road of technology for migrant workers (jishuroad) , author : Click to follow ????
The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .
Original publication time : 2021-01-27
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .