linux/unix命令详解之cut

Comments: 2 Comments
Published on: 2012 年 01 月 04 日

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.
       Mandatory arguments to long options are mandatory for short options too.
       -b, --bytes=LIST
              output only these bytes
       -c, --characters=LIST
              output only these characters
       -d, --delimiter=DELIM
              use DELIM instead of TAB for field delimiter
       -f, --fields=LIST
              output only these fields;  also print any line that contains no delimiter character, unless the -s option is
              specified
       -n     with -b: don’t split multibyte characters
       -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
       --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.
       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

cut 命令主要用于行内数据的选择和获取。获取方式有三种:
1、-b 以字节为单位的获取
2、-c 以字符为单位的获取
3、-f 以字段为单位的获取
这三个选项后边都是跟LIST列表,列表的格式主要有4种:
1、N 第N个字节、字符或者字段
2、N- 从第N个字节、字符或者字段到行末
3、N-M 从第N个字节、字符或者字段到第M个
4、-M 从行首到第M个字节、字符或者字段
N、M均为闭区间。

cut可以从文件获取数据也可以从标准输入获取数据。使用-d '分隔符' 来确定数据的分隔符,默认的是\t,空格什么的都不是默认分隔符。

-s选项只有行内有指定分隔符的才处理
--output-delimiter=STRING 输出结果的分隔符指定为STRING
-n 选项不分开多字节字符

实例:
cut -b 2-3 #获取第2到3个字节
cut -d" " --output-delimiter="xxx" -f 1-3 #指定分隔符位空格,获取1-3个字段并用xxx来做分隔符

我猜你可能也喜欢:

2 Comments - Leave a comment
  1. lea说道:

    quot "使用-d ‘分隔符’ 来确定数据的分隔符,默认的是\t"

    \t stands for what?

    thanks

Leave a comment

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Welcome , today is 星期二, 2017 年 10 月 24 日