bash 使用 cut 命令删除多列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13690461/
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
Using cut command to remove multiple columns
提问by user121196
given input
给定输入
echo 1,2,3,4,5,6,7,8,9,...100
If I want to cut columns 5 I can do
如果我想剪切第 5 列,我可以做到
cut -d, -f-4,6-
what if I want to cut multiple non consecutive columns like 5, 7,etc is there a one liner?
如果我想切割多个非连续列(如 5、7 等),是否有一个衬里?
回答by newfurniturey
You should be able to continue the sequences directly in your existing -f
specification.
您应该能够直接在现有-f
规范中继续序列。
To skip both 5 and 7, try:
要同时跳过 5 和 7,请尝试:
cut -d, -f-4,6-6,8-
As you're skipping a single sequential column, this can also be written as:
当您跳过单个连续列时,这也可以写为:
cut -d, -f-4,6,8-
To keep it going, if you wanted to skip 5, 7, and 11, you would use:
为了继续下去,如果你想跳过 5、7 和 11,你可以使用:
cut -d, -f-4,6-6,8-10,12-
To put it into a more-clear perspective, it is easier to visualize when you use starting/ending columns which go on the beginning/end of the sequence list, respectively. For instance, the following will print columns 2 through 20, skipping columns 5 and 11:
从更清晰的角度来看,当您使用分别位于序列列表开头/结尾的开始/结束列时,更容易形象化。例如,以下将打印第 2 到 20 列,跳过第 5 和 11 列:
cut -d, -f2-4,6-10,12-20
So, this will print "2 through 4", skip 5, "6 through 10", skip 11, and then "12 through 20".
因此,这将打印“2 到 4”,跳过 5,“6 到 10”,跳过 11,然后是“12 到 20”。
回答by Chris Johnson
Sometimes it's easier to think in terms of which fields to exclude.
有时更容易考虑要排除哪些字段。
If the number of fields not being cut (not being retained in the output) is small, it may be easier to use the --complement
flag, e.g. to include all fields 1-20 except not 3, 7, and 12 -- do this:
如果未剪切(未保留在输出中)的字段数量很少,则使用该--complement
标志可能更容易,例如,包括除 3、7 和 12 之外的所有字段 1-20 - 执行以下操作:
cut -d, --complement -f3,7,12 <inputfile
Rather than
而不是
cut -d, -f-2,4-6,8-11,13-
回答by sge
You are able to cut all odd/even columns by using seq:
您可以使用 seq 剪切所有奇数/偶数列:
This would print all odd columns
这将打印所有奇数列
echo 1,2,3,4,5,6,7,8,9,10 | cut -d, -f$(seq -s, 1 2 10)
To print all even columns you could use
要打印您可以使用的所有偶数列
echo 1,2,3,4,5,6,7,8,9,10 | cut -d, -f$(seq -s, 2 2 10)
By changing the second number of seq you can specify which columns to be printed.
通过更改 seq 的第二个数量,您可以指定要打印的列。
If the specification which columns to print is more complex you could also use a "one-liner-if-clause" like
如果要打印的列的规范更复杂,您还可以使用“one-liner-if-clause”,如
echo 1,2,3,4,5,6,7,8,9,10 | cut -d, -f$(for i in $(seq 1 10); do if [[ $i -lt 10 && $i -lt 5 ]];then echo -n $i,; else echo -n $i;fi;done)
This would print all columns from 1 to 5 - you can simply modify the conditions to create more complex conditions to specify weather a column shall be printed.
这将打印从 1 到 5 的所有列 - 您可以简单地修改条件以创建更复杂的条件来指定应打印列的天气。
回答by Chris Koknat
The same could be done with Perl
Because it uses 0-based-indexing instead of 1-based-indexing, the field values are offset by 1
Perl 也可以这样做
因为它使用基于 0 的索引而不是基于 1 的索引,所以字段值偏移了 1
perl -F, -lane 'print join ",", @F[1..3,5..9,11..19]'
is equivalent to:
相当于:
cut -d, -f2-4,6-10,12-20
If the commas are not needed in the output:
如果输出中不需要逗号:
perl -F, -lane 'print "@F[1..3,5..9,11..19]"'