龙空技术网

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

云中漫步 116

前言:

目前小伙伴们对“c语言文件删除部分内容举例”大约比较关注,你们都想要知道一些“c语言文件删除部分内容举例”的相关资讯。那么小编也在网上搜集了一些关于“c语言文件删除部分内容举例””的相关知识,希望朋友们能喜欢,同学们一起来了解一下吧!

简介

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.txtapplesbananaspearsmongosorageswatermelonhello worldhello againyunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b 1 friutes.txtabpmowhhyunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b 1,3,5 friutes.txtapebnnpasmnooaewtrhlohlo
2 使用cut命令选择连续范围内的字节

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

命令格式:

$ cut -b N-M file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cat friutes.txtapplesbananaspearsmongosorageswatermelonhello worldhello againyunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b 6-9 friutes.txtsasssmelo wor aga

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

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b 1-5 friutes.txt test.txtapplebananpearsmongooragewaterhellohelloYou hHelloprint
3 打印选中的字符

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

命令格式:

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

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -c 1-5 test.txtYou hHelloprint遇▒yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -c 1-6 test.txtYou haHelloprint遇到yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cat test.txtYou 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.txtYou have my attention!Hello world.print the world.遇到中文怎么办?yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut --field 1 test.txtYou 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.txtYou have my attention!Hello world.print the world.遇到中文怎么办?分割yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -c 2- test.txtou have my attention!ello world.rint the world.▒▒到中文怎么办?▒▒行用TAB       分割yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b 2- test.txtou 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.txt2You     have    my      attention!Hello   world.print the       world.yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -c -1 test.txt2YHpyunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -c -2 test.txt2YoHepryunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b -2 test.txt2YoHepryunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -f -2 test.txt2You     haveHello   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.txtapplesbananaspearsmongosorageswatermelonhello worldhello againyunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -d ' ' -f 2 friutes.txtapplesbananaspearsmongosorageswatermelonworldagainyunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -d a -f 2 friutes.txtpplesnrsmongosgestermelonhello worldg
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.txtYou     have    my      attention!Hello   world.print the       world.遇到中文怎么办?本行用TAB       分割yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -d : -sf 2 test.txtyunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$
9 打印被筛掉的信息

--complement参数可以让我们打印被cut命令切掉的信息。

它可以配合参数-b, -c, 或者 -f使用。

命令格式:

$ cut --complement -b N file

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b -2 friutes.txtapbapemoorwaheheyunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut -b -2 --complement friutes.txtplesnanasarsngosagestermelonllo worldllo again
10 打印cut命令的版本

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

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/cutdir$ cut --versioncut (GNU coreutils) 8.30Copyright (C) 2018 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <;.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

标签: #c语言文件删除部分内容举例