Linux 如何剪切前 n 列和后 n 列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4956873/
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-08-04 00:18:28  来源:igfitidea点击:

How to cut first n and last n columns?

linuxbashshell

提问by haluk

How can I cut off the first nand the last ncolumns from a tab delimited file?

我怎样才能切断第一ñ最后ñ从制表符分隔文件列?

I tried this to cut first ncolumn. But I have no idea to combine first and last n column

我试着用这个来切割第一个n列。但我不知道将第一列和最后 n 列结合起来

cut -f 1-10 -d "<CTR>v <TAB>" filename

采纳答案by kauppi

Cut can take several ranges in -f:

Cut 可以采用以下几个范围-f

Columns up to 4 and from 7 onwards:

最多 4 列和从 7 列开始:

cut -f -4,7-

or for fields 1,2,5,6 and from 10 onwards:

或对于字段 1、2、5、6 和从 10 开始:

cut -f 1,2,5,6,10-

etc

等等

回答by kurumi

you can use awk, for example, cut off 1st,2nd and last 3 columns

您可以使用awk,例如,截断第1、第2和最后3列

awk '{for(i=3;i<=NF-3;i++} print $i}' file

if you have a programing language such as Ruby (1.9+)

如果你有一种编程语言,比如 Ruby (1.9+)

$ ruby -F"\t" -ane 'print $F[2..-3].join("\t")' file

回答by Paused until further notice.

To use AWK to cut off the first and last fields:

要使用 AWK 截断第一个和最后一个字段:

awk '{ = ""; $NF = ""; print}' inputfile

Unfortunately, that leaves the field separators, so

不幸的是,这留下了字段分隔符,所以

aaa bbb ccc

becomes

变成

[space]bbb[space]

To do this using kurumi's answer which won't leave extra spaces, but in a way that's specific to your requirements:

要做到这一点,请使用 kurumi 的答案,它不会留下额外的空间,而是以特定于您的要求的方式:

awk '{delim = ""; for (i=2;i<=NF-1;i++) {printf delim "%s", $i; delim = OFS}; printf "\n"}' inputfile

This also fixes a couple of problems in that answer.

这也解决了该答案中的几个问题。

To generalize that:

概括地说:

awk -v skipstart=1 -v skipend=1 '{delim = ""; for (i=skipstart+1;i<=NF-skipend;i++) {printf delim "%s", $i; delim = OFS}; printf "\n"}' inputfile

Then you can change the number of fields to skip at the beginning or end by changing the variable assignments at the beginning of the command.

然后,您可以通过更改命令开头的变量分配来更改要在开头或结尾跳过的字段数。

回答by user2009292

Try the following:

请尝试以下操作:

echo a#b#c | awk -F"#" '{ = ""; $NF = ""; print}' OFS=""

回答by kenorb

You can use Bash for that:

您可以为此使用 Bash:

while read -a cols; do echo ${cols[@]:0:1} ${cols[@]:1,-1}; done < file.txt

回答by Saurabh

You can cut using following ,
-d: delimiter ,-f for fields
\t used for tab separated fields

您可以使用以下 ,
-d: delimiter ,-f剪切字段
\t 用于制表符分隔的字段

cut -d$'\t' -f 1-3,7-

回答by Mark G.

The first part of your question is easy. As already pointed out, cut accepts omission of either the starting or the ending index of a column range, interpreting this as meaning either “from the start to column n(inclusive)” or “from column n(inclusive) to the end,” respectively:

你问题的第一部分很简单。正如已经指出的, cut 接受省略列范围的开始或结束索引,将其解释为“从开始到第n列(包含)”或“从第n列(包含)到结束”,分别:

$ printf 'this:is:a:test' | cut -d: -f-2
this:is
$ printf 'this:is:a:test' | cut -d: -f3-
a:test

It also supports combiningranges. If you want, e.g., the first 3 and the last 2 columns in a row of 7 columns:

它还支持组合范围。如果你想要,例如,一行 7 列中的前 3 列和最后 2 列:

$ printf 'foo:bar:baz:qux:quz:quux:quuz' | cut -d: -f-3,6-
foo:bar:baz:quux:quuz

However,the second part of your question can be a bit trickier depending on what kind of input you're expecting. If by “last ncolumns” you mean “last ncolumns (regardless of their indices in the overall row)” (i.e. because you don't necessarily know how many columns you're going to find in advance) then sadly this is not possible to accomplish using cutalone. In order to effectively use cutto pull out “the last ncolumns” in each line, the total numberof columns present in each line must be known beforehand, andeach line must be consistent in the number of columns it contains.

但是,您的问题的第二部分可能会有点棘手,具体取决于您期望的输入类型。如果“最后n列”的意思是“最后n列(无论它们在整个行中的索引如何)”(即因为您不一定知道要提前找到多少列),那么遗憾的是这不是可以cut单独使用。为了有效地使用cut拉出每行中的“最后n列”,必须事先知道每行中存在的并且每行包含的列数必须一致。

If you do notknow how many “columns” may be present in each line (e.g. because you're working with input that is not strictly tabular), then you'll have to use something like awkinstead. E.g., to use awkto pull out the last 2 “columns” (awk calls them fields,the number of which can vary per line) from each line of input:

如果您知道每行中可能存在多少“列”(例如,因为您正在处理不是严格表格的输入),那么您将不得不使用类似的东西awk。例如,用于从每行输入中awk提取最后 2 个“列”(awk 称它们为字段,每行的数量可能不同):

$ printf '/a\n/a/b\n/a/b/c\n/a/b/c/d\n' | awk -F/ '{print $(NF-1) FS $(NF)}'
/a
a/b
b/c
c/d

回答by Yu Tao

Use

cut -b COLUMN_N_BEGINS-COLUMN_N_UNTIL INPUT.TXT > OUTPUT.TXT

-fdoesn't work if you have "tabs" in the text file.

-f如果文本文件中有“标签”,则不起作用。