MySQL, Oracle, Linux, 软件架构及大数据技术知识分享平台

网站首页 > linux / 正文

linux也有砍一刀的功能,用命令cut砍掉文件多余内容

2024-11-26 16:37 huorong linux 4 ℃ 0 评论


简介

cut是一个命令行实用程序,用于从Linux系统中的每一行文件中删除部分。它将每个文件中选定的行部分打印到标准输出。
用户必须为cut命令指定字节、字符或字段的列表。因此,cut只在使用-b、-c或-f选项时有效。
可以通过以下的方式指定字节、字符或者字段列表:

N :N个字节、字符或者字段。从1开始。
N-M : N到M个字节、字符或者字段。
N- : 从第N个字节、字符或者字段开始。
-M :从第一个到第M个字节、字符或者字段。

示例

cut命令的语法格式:

$ cut OPTION... [FILE]...

1 使用cut命令只打印选择的字节

使用参数-b 或者 --bytes 选择需要的字节并打印到终端。
命令格式:

$ cut -b N file
或者
$ cut --bytes=N file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cat friutes.txt
apples
bananas
pears
mongos
orages
watermelon
hello world
hello again
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b 1 friutes.txt
a
b
p
m
o
w
h
h
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b 1,3,5 friutes.txt
ape
bnn
pas
mno
oae
wtr
hlo
hlo

2 使用cut命令选择连续范围内的字节

可以使用‘-’,选择连续范围内的字节。TAB和空格都被记为一个字节。
命令格式:

$ cut -b N-M file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cat friutes.txt
apples
bananas
pears
mongos
orages
watermelon
hello world
hello again
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b 6-9 friutes.txt
s
as

s
s
melo
 wor
 aga

当有多个文件需要操作时,cut命令可以让操作更加快捷:一次指定多个文件。

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b 1-5 friutes.txt test.txt
apple
banan
pears
mongo
orage
water
hello
hello
You h
Hello
print

3 打印选中的字符

类似于字节,我们可以使用-c 或者 --characters 参数,指定字符并打印出来。TAB和空格都记为一个字符。
命令格式:

$ cut -c N file
或者
$ cut --characters=N file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -c 1-5 test.txt
You h
Hello
print
遇?
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -c 1-6 test.txt
You ha
Hello
print
遇到
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cat test.txt
You have my attention!
Hello world.
print the world.
遇到中文怎么办?

从上面的例子可以看出,中文打印会出现乱码的情况,因为一个中文需要多个字节标识,对应到字符,也被认为是多个字符。

4 打印选择的字段

参数-f 或者 fields 可以指定字段。 这种方式还打印不包含分隔符的任何行。 分隔符是标记一行的开始和结束的字符。TAB是默认的字段分隔符。
命令格式:

$ cut -f N file
或者
$ cut --field N file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -f 1 test.txt
You have my attention!
Hello world.
print the world.
遇到中文怎么办?
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut --field 1 test.txt
You have my attention!
Hello world.
print the world.
遇到中文怎么办?

5 打印从第N个字节、字符或字段到结束

当打印字节时,命令格式:

$ cut -b N- file

当需要指定字符时,命令格式:

$ cut -c N- file

当需要指定字段时,命令格式:

$ cut -f N- file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -f 2- test.txt
You have my attention!
Hello world.
print the world.
遇到中文怎么办?
分割
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -c 2- test.txt
ou have my attention!
ello world.
rint the world.
??到中文怎么办?
??行用TAB       分割
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b 2- test.txt
ou have my attention!
ello world.
rint the world.
??到中文怎么办?
??行用TAB       分割

6 打印前M个字符、字节或字段

按照不同的参数,都可以指定前M个:

$ cut -b -M file
$ cut -c -M file
$ cut -f -M file

示例

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cat test.txt2
You     have    my      attention!
Hello   world.
print the       world.
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -c -1 test.txt2
Y
H
p
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -c -2 test.txt2
Yo
He
pr
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b -2 test.txt2
Yo
He
pr
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -f -2 test.txt2
You     have
Hello   world.
print the       world.

7 使用不同的分隔符

默认情况下,cut命令使用TAB作为分隔符。参数-d 或者 --delimiter可以让用户指定分隔符。分隔符必须为一个字符。
命令格式:

$ cut -d DELIM -f N file
或者
$ cut --delimiter=DELIM -f N file

示例,使用空格作为分隔符需要通过''指定,其他的字符可以直接写:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cat friutes.txt
apples
bananas
pears
mongos
orages
watermelon
hello world
hello again
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -d ' ' -f 2 friutes.txt
apples
bananas
pears
mongos
orages
watermelon
world
again
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -d a -f 2 friutes.txt
pples
n
rs
mongos
ges
termelon
hello world
g

8 只打印带分隔符的行

默认情况下,如果一行不存在分隔符,这一行全部信息都会被打印出来。我们可以使用参数-s 或者 --only-delimited,让-f参数打印信息只包含哪些有分隔符的行。
命令格式:

$ cut -sf n file
或者
$ cut --only-delimited -f n file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -d : -f 2 test.txt
You     have    my      attention!
Hello   world.
print the       world.
遇到中文怎么办?
本行用TAB       分割
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -d : -sf 2 test.txt
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$

9 打印被筛掉的信息

--complement参数可以让我们打印被cut命令切掉的信息。
它可以配合参数-b, -c, 或者 -f使用。
命令格式:

$ cut --complement -b N file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b -2 friutes.txt
ap
ba
pe
mo
or
wa
he
he
yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b -2 --complement friutes.txt
ples
nanas
ars
ngos
ages
termelon
llo world
llo again

10 打印cut命令的版本

可以使用参数--version打印cut版本。
示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut --version
cut (GNU coreutils) 8.30
Copyright (C) 2018 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 permitted by law.

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

11 查看帮助信息

使用参数--help,查看cut命令的帮助信息,获得本文实例之外的用法。

cut --help

Tags:linux删除文件的命令

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言