Linux Classes
Linux Training
* Linux Classes *

LINUX CLASSES - DATA MANIPULATION

How do I Select Columns From a File?

The cut command takes a vertical slice of a file, printing only the specified columns or fields. Like the sort command, the cut command defines a field as a word set off by blanks, unless you specify your own delimiter. It's easiest to think of a column as just the nth character on each line. In other words, "column 5" consists of the fifth character of each line.

Consider a slight variation on the company.data file we've been playing with in this section:

406378:Sales:Itorre:Jan
031762:Marketing:Nasium:Jim
636496:Research:Ancholie:Mel
396082:Sales:Jucacion:Ed

If you want to print just columns 1 to 6 of each line (the employee serial numbers), use the -c1-6 flag, as in this command:

cut -c1-6 company.data
406378
031762
636496
396082

If you want to print just columns 4 and 8 of each line (the first letter of the department and the fourth digit of the serial number), use the -c4,8 flag, as in this command:

cut -c4,8 company.data
3S
7M
4R
0S

And since this file obviously has fields delimited by colons, we can pick out just the last names by specifying the -d: and -f3 flags, like this:

cut -d: -f3 company.data
Itorre
Nasium
Ancholie
Jucacion

It's often the case that you want to use a space as the delimiter. To do so, you must put the delimiter in single quotes, like this: -d' '

Also, when you want to cut from a starting point to the end of the line, just leave off the final field number, as shown in the example below.

Let's say this is your test.txt file:
abc def ghi jkl
mno pqr stu vwx
yz1 234 567 890

To cut only columns 2-END, do this: cut -d' ' -f2- test.txt

And the results are:
def ghi jkl
pqr stu vwx
234 567 890

Here is a summary of the most common flags for the cut command:

-c [n | n,m | n-m] Specify a single column, multiple columns (separated by a comma), or range of columns (separated by a dash).
-f
[n | n,m | n-m] Specify a single field, multiple fields (separated by a comma), or range of fields (separated by a dash).
-d
c Specify the field delimiter.
-s
Suppress (don't print) lines not containing the delimiter.

Previous Lesson: Eliminating Duplicates
Next Lesson: Selecting Records

[ RETURN TO INDEX ]

Comments (most recent first)

shoaibkhan     (02 Sep 2010, 12:17)
great explanation sir.
kalai     (31 Jul 2010, 00:24)
how to cut a single line in unix using cut command
Reyhaneh     (07 Jul 2010, 03:57)
Hi, I want to change the delimiter from <tab> to space. how can I introduce <tab> in sed command?
Adnan     (01 Jul 2010, 10:41)
very good ..now i know much about cut command......
Cape     (01 Jul 2010, 06:08)
Shure it helps, I will use that way.
Thank you so much for your help.
See you...
john.ndambuki     (30 Jun 2010, 17:16)

@Cape: OK I see what you mean now... I'm not sure how to do this with cut ( I guess I would also like to know this :) ), but if you have awk on your system you could try:

ls -l | awk -F" " '{print $<field>}'

OR simply

ls -l | awk '{print $<field>}'

Replace <field> with whichever column/field you would like to print out. All white spaces are treated equally.

e.g.

ls -l | awk -F" " '{print $6}'

OR

ls -l | awk '{print $6}'

Hope this helps!
Cape     (30 Jun 2010, 07:58)
Thank you for your help John.
But what I am trying to do is :
ls -l | cut -f6
to get only de size of all files in the directory (for example) but the command ls -l steel show me all collumns.
Thanks again.
john.ndambuki     (30 Jun 2010, 07:20)

@Jan - you're welcome!

@Cape - the default delimiter is <TAB> so you could, for example, run the same command without the '-d' option i.e.:

cut -f1-3 <filename>

this will give you the first 3 columns, where the delimiter is <TAB>.
Cape     (30 Jun 2010, 05:33)
A simple question: How do you tell cut command that you want to use a <tab> as a column delimiter.
Thank you very much.
Jas     (30 Jun 2010, 05:18)
Thank you Johan! for your short and best resolution...
john.ndambuki     (30 Jun 2010, 03:18)
@Jas

You could used:

cut -d' ' -f1-3 <filename>

this will give you the first 3 columns, where the delimiter is <space> as specified by -d' '
Jas     (23 Jun 2010, 06:22)
To cut the last coulumn after last delimiter what is the syntax is used.

For eg in above mentioned example :
file is
abc def ghi jkl
mno pqr stu vwx
yz1 234 567 890

if we wana result like

abc def ghi
mno pqr stu
yz1 234 567

Then what command need to be use..
netman     (07 Jun 2010, 00:15)
thank for your good resource.
Cape     (02 Jun 2010, 09:22)
What about a <TAB> delimiter, like in an ls -l command where "user name" is variable in size.
I hope you understand me, thanks a lot.
Bob Rankin     (26 May 2010, 07:56)
It's all about identifying the delimiters and which fields you want. In this case, the command I gave before still works.
Rebecca     (26 May 2010, 07:33)
Thank you.. i guess my problem was actually more like:
ABC def.ghi.jkl
MNO pqr.stu.vwx

and i wanted ghi. Help is much appreciated!
Bob Rankin     (25 May 2010, 16:58)
The delimiter is '.' and you want field 2. So:

cut -d'.' -f2
Rebecca     (25 May 2010, 15:24)
What if I have:
def.ghi.jkl
pqr.stu.vwx
234.567.890

and i only want ghi?
Paul     (23 May 2010, 18:52)
Thank you for the short and simple examples.
muath     (20 May 2010, 22:12)
in terminal it works fine,,
but i tried to use it in user defined script ,,,but is it didn't work ..how i can use in user defined script to give me the same result....?
i really need it in a my own script..
can you help me...
Singh     (20 May 2010, 20:26)
Thanks a lot sir. very clear information unlike most of the books where you get tired of reading long text and figuring out what actually author want to explain. After reading in many books the same thing still is not clear . But this is very practical and nice information.
Vijay     (20 May 2010, 06:36)
I cant't find such explanation of linux commands in any other website.This is really awesome.
nick the greek     (07 May 2010, 11:57)
thnx alot
Bob Rankin     (23 Apr 2010, 06:51)
Let's day this is your file:
abc def ghi jkl
mno pqr stu vwx
yz1 234 567 890

To cut only columns 2-END, do this:
cut -d' ' -f2- tt

And the results are:
def ghi jkl
pqr stu vwx
234 567 890
Dennis     (23 Apr 2010, 01:02)
How to cut from column a to the end of line?
SA     (22 Apr 2010, 03:26)
awsome
ayush     (14 Apr 2010, 04:27)
how do u delimit <space>
Ammu     (29 Mar 2010, 06:12)
good description with easy examples
ice     (29 Mar 2010, 04:09)
Thank uuuuuu
Dennis     (17 Mar 2010, 04:17)
good information
Michael     (02 Mar 2010, 02:05)
Awesome summary of commands. Cheers!
jeevan     (26 Feb 2010, 08:09)
its good. I got enough info from this thank you so much.

*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 

Ask Bob Rankin - Free Tech Support
<Send This Link to a Friend>         <Bookmark This Page>


Copyright © by Bob Rankin
All rights reserved - Redistribution is allowed only with permission.