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?
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.
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.
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.
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.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With