shell脚本---常用命令cut

  • 用途

cut - remove sections from each line of files

从每一行中取的选中块。要理解块,首先要了解一行内容如何划分为多个session。一行是通过分隔符划分为多个块。比如:
Print selected parts
假如分割符是空格,块就有3块,Print ->  块1 selected->块2 parts->块3

  • 用法举例

1. -b, --bytes=LIST

   select only these bytes 表示字节为单位进行计算。
   -b1-3 表示取这行的第1,2,3个字节
   -b1-3,9-12表示取这行的第1,2,3个字节和第9,10,11字节

renhl@verygood:~/usb$ cat 1.txt 
abc     def     ghi
012     456     789

renhl@verygood:~/usb$ cut -b1-3,9-11 1.txt #显示第1,2,3字节和第9,10,11字节
abcghi
012789

2. -c, --characters=LIST

    select only these characters 与上面的byte类似。 

renhl@verygood:~/usb$ cut -c1,2 1.txt #显示第1,2个字符
ab
01

3.  -d, --delimiter=DELIM

      use DELIM instead of TAB for field delimiter。
      默认的分割符是TAB,如果要制定其他的分割符需要使用-d。一般与-f配套使用。          

renhl@verygood:~/usb$ cut -f 1,2 1.txt  #分隔符默认是TAB,显示1,2两块
abc     def
012     456
renhl@verygood:~/usb$ cut -d e -f 1 1.txt #指定分隔符为e,显示第1块,不匹配的第2行也会显示
abc     d
012     456     789
renhl@verygood:~/usb$ cut -d e -f 1 -s 1.txt #指定分隔符为e,显示第1块,不匹配的第2行不会显示
abc     d
renhl@verygood:~/usb$ cut -d e -f 2 -s 1.txt #指定分隔符为e,显示第2块,不匹配的第2行不会显示
f       ghi

4.   -f, --fields=LIST

       select  only these fields;  also print any line that contains no delimiter character, unless the -s option is specified
       参照“-d, --delimiter=DELIM”章节。

5. --complement

complement the set of selected bytes, characters or fields。
显示与条件相反的块

renhl@verygood:~/usb$ cut -f 1,2 1.txt #显示第1,2块
abc     def
012     456
renhl@verygood:~/usb$ cut -f 1,2 --complement 1.txt #显示除了第1,2块之外的块
ghi
789

6. -s, --only-delimited 

    do not print lines not containing delimiter
    参考 “-d, --delimiter=DELIM”和“ -f, --fields=LIST”章节的描述。

7. --output-delimiter=STRING

   use STRING as the output delimiter the default is to use the input delimiter
   输出的输出连接符号   

renhl@verygood:~/usb$ cut -f 1,2 1.txt
abc     def
012     456
renhl@verygood:~/usb$ cut -f 1,2 --output-delimiter=# 1.txt #使用#作为输出连接符
abc#def

8.    -z, --zero-terminated

   line delimiter is NUL, not newline
   指定行结束符为其他字符   

renhl@verygood:~/usb$ cat 1.txt
abc     def     ghi
012     456     789

renhl@verygood:~/usb$ cut -f 1 -z 1.txt
abc

renhl@verygood:~/usb$ cut -f 2 -z 1.txt
def

renhl@verygood:~/usb$ cut -f 3 -z 1.txt # -z将多行看成一行,注意3是两行字符作为一个
ghi
012

renhl@verygood:~/usb$ cut -f 4 -z 1.txt
456

renhl@verygood:~/usb$ cut -f 5 -z 1.txt
789
  • cut帮助文件

CUT(1)                                              User Commands                                              CUT(1)

NAME
       cut - remove sections from each line of files

SYNOPSIS
       cut OPTION... [FILE]...

DESCRIPTION
       Print selected parts of lines from each FILE to standard output.

       With no FILE, or when FILE is -, read standard input.

       Mandatory arguments to long options are mandatory for short options too.

       -b, --bytes=LIST
              select only these bytes

       -c, --characters=LIST
              select only these characters

       -d, --delimiter=DELIM
              use DELIM instead of TAB for field delimiter

       -f, --fields=LIST
              select  only these fields;  also print any line that contains no delimiter character, unless the -s option is specified

       -n     (ignored)

       --complement
              complement the set of selected bytes, characters or fields

       -s, --only-delimited
              do not print lines not containing delimiters

       --output-delimiter=STRING
              use STRING as the output delimiter the default is to use the input delimiter

       -z, --zero-terminated
              line delimiter is NUL, not newline

       --help display this help and exit

       --version
              output version information and exit

       Use one, and only one of -b, -c or -f.  Each LIST is made up of one range, or many ranges separated by commas.
       Selected  input  is written in the same order that it is read, and is written exactly once.  Each range is one
       of:

       N      N'th byte, character or field, counted from 1

       N-     from N'th byte, character or field, to end of line

       N-M    from N'th to M'th (included) byte, character or field

       -M     from first to M'th (included) byte, character or field

AUTHOR
       Written by David M. Ihnat, David MacKenzie, and Jim Meyering.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright  ©  2020  Free  Software  Foundation,  Inc.   License  GPLv3+:  GNU   GPL   version   3   or   later
       <https://gnu.org/licenses/gpl.html>.
       This  is  free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent per‐
       mitted by law.

SEE ALSO
       Full documentation <https://www.gnu.org/software/coreutils/cut>
       or available locally via: info '(coreutils) cut invocation'

GNU coreutils 8.32                                  February 2022                                              CUT(1)

  • 19
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值