How to remove columns from a text file

Problem

I want to remove the 1st and the 5th columns of a text file of tab separated values. The file is as follows:

       
HEAD1 HEAD2 HEAD3 HEAD4 HEAD5 HEAD6
1.0   -3    3.4   2     7     9
-100  -30   -1    0     -80   7
70    7     8     0     3     0
71    4.5   2.1   0.6   37    4

Solution

The easiest way is to use cut and with the -f option write a list of the columns to maintain:

$ cut -f 2-4,6- test.txt > outfile.txt

This will create the outfile.txt file:

HEAD2 HEAD3 HEAD5 HEAD6
-3    3.4   7     9
-30   -1    -80   7
7     8     3     0
4.5   2.1   37    4

If instead of having tab separated columns you have the columns separated by other columns, you can specify it with -d:

$ cut -d "," -f 2-4,6- test.txt
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s