Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cut command to remove multiple columns

Tags:

bash

cut

given input

  echo 1,2,3,4,5,6,7,8,9,...100  

If I want to cut columns 5 I can do

 cut -d, -f-4,6- 

what if I want to cut multiple non consecutive columns like 5, 7,etc is there a one liner?

like image 201
user121196 Avatar asked Dec 03 '12 19:12

user121196


People also ask

How cut command is used in Linux with example?

The cut command in UNIX is a command for cutting out the sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character and field. Basically the cut command slices a line and extracts the text.

How do I cut a specific column in a Unix file?

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 you cut a column in Linux?

Select Column of Characters using either Start or End Position. Either start position or end position can be passed to cut command with -c option.

How do you delete a column from a file in Linux?

Use the colrm command to remove specified columns from a file. Input is taken from standard input. Output is sent to standard output. If the command is called with one parameter, the columns of each line from the specified column to the last column are removed.


1 Answers

You should be able to continue the sequences directly in your existing -f specification.

To skip both 5 and 7, try:

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:

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:

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".

like image 96
newfurniturey Avatar answered Oct 22 '22 06:10

newfurniturey