如何在 bash 中使用 cut 命令显示除指定列之外的所有列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15353655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:26:13  来源:igfitidea点击:

How to use cut command in bash to show all columns except those indicated?

bashcut

提问by Herman

I need to remove a column from a plain text file. I think this could be done using the inverse of the cut command. I mean, something like this:

我需要从纯文本文件中删除一列。我认为这可以使用 cut 命令的逆来完成。我的意思是,像这样:

If this is my file:

如果这是我的文件:

01 Procedimiento_tal retiro aceptado
01 tx1
01 tx2
01 tx3
02 Procedimiento_tal retiro rechazado
02 tx1
02 tx2
02 tx3
03 Procedimiento_tal retiro aceptado
03 tx1
03 tx2
03 tx3

What can I do to remove the first column with cut and get the following text in bash?:

我该怎么做才能用 cut 删除第一列并在 bash 中获得以下文本?:

Procedimiento_tal retiro aceptado
tx1
tx2
tx3
Procedimiento_tal retiro rechazado
tx1
tx2
tx3
Procedimiento_tal retiro aceptado
tx1
tx2
tx3

Thanks in advance

提前致谢

回答by William Pursell

Using cut:

使用cut

cut -d ' ' -f 2- input-file

should do what you want.

应该做你想做的。

回答by Johnsyweb

To read infileusing ' 'as a delimiter (-d) and put fields (-f) 2 onwards (2-) into file:

读取infileusing' '作为分隔符 ( -d) 并将字段 ( -f) 2 向前 ( 2-) 放入file

cut -d' ' -f2- infile > file

See man cutfor more options.

查看man cut更多选项。

N.B:This is not bash-specific.

注意:这不是特定于bash 的

回答by Thor

GNU coreutils cut supports the --complementflag. So if you are using that version of cut you can say:

GNU coreutils cut 支持该--complement标志。所以如果你使用那个版本的 cut 你可以说:

cut --complement -d' ' -f1 infile

Output:

输出:

Procedimiento_tal retiro aceptado
tx1
tx2
tx3
Procedimiento_tal retiro rechazado
tx1
tx2
tx3
Procedimiento_tal retiro aceptado
tx1
tx2
tx3

回答by Flare Cat

Here is a command that works, and that is a bit shorter than everyone's:

这是一个有效的命令,它比每个人的都短一点:

cut -b 4- input-file

-b stands for byte, and selects the bytes that will get pasted to the screen.

-b 代表字节,并选择将被粘贴到屏幕的字节。

4- is a part of -b and it means that it will select the 4th character of every row to the last character of every row. the cut command will then paste the selection to the screen.

4- 是 -b 的一部分,这意味着它将选择每行的第 4 个字符到每行的最后一个字符。然后剪切命令会将选择粘贴到屏幕上。