Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cut first n and last n columns?

Tags:

linux

bash

shell

How can I cut off the first n and the last n columns from a tab delimited file?

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

cut -f 1-10 -d "<CTR>v <TAB>" filename 
like image 945
haluk Avatar asked Feb 10 '11 12:02

haluk


People also ask

How do you cut the first two fields in Unix?

1) The cut command is used to display selected parts of file content in UNIX. 2) The default delimiter in cut command is "tab", you can change the delimiter with the option "-d" in the cut command. 3) The cut command in Linux allows you to select the part of the content by bytes, by character, and by field or column.

How do I cut a specific column in Linux?

-c (column): To cut by character use the -c option. This can be a list of numbers separated comma or a range of numbers separated by hyphen(-).

How Cut command works Unix?

The cut command is a command-line utility that allows you to cut out sections of a specified file or piped data and print the result to standard output. The command cuts parts of a line by field, delimiter, byte position, and character.

What does cut command do in Linux?

The cut command is a command-line utility for cutting sections from each line of a file. It writes the result to the standard output. It's worth noting that it does not modify the file, but only works on a copy of the content.


2 Answers

Cut can take several ranges in -f:

Columns up to 4 and from 7 onwards:

cut -f -4,7- 

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

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

etc

like image 156
kauppi Avatar answered Sep 26 '22 06:09

kauppi


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:

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

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

$ 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 n columns” you mean “last n columns (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 cut alone. In order to effectively use cut to pull out “the last n columns” in each line, the total number of columns present in each line must be known beforehand, and each line must be consistent in the number of columns it contains.

If you do not know 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 awk instead. E.g., to use awk to pull out the last 2 “columns” (awk calls them fields, the number of which can vary per line) from each line of input:

$ 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 
like image 22
Mark G. Avatar answered Sep 23 '22 06:09

Mark G.