As the doctor gone rogue

March 2, 2011

Using cut to extract specific columns from a fixed-width format file

Filed under: bash, data management — Tags: , , , , — hypotheses @ 12:07 pm

Occasionally, I will have to deal with a text file in a fixed width text format. In Linux, you can extract specific columns easily using cut

cut -b 1-10,15-20 < infile

This will give you column 1-10 and 15-20 of your “infile”

The additional option that is nice when you want to get rid of a few columns and keep the rest of them is using the option –complement (although from what I have heard, some systems might not have this implemented).

cut –complement -b 11,14,32,43-47,58-62,73-77,88-92,103-108 < infile

Other use of cut is to extract columns from any other type of file with delimiter such as “,” or space

by adding the option -d”_your_delim_” to the example above, and you can extract your infile.csv or infile.txt as well.

 

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: