Linux Classes
Share This With a Friend  
LINUX CLASSES - DATA MANIPULATION

Linux Cut Command

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
(Please feel free to answer questions posted by others!)

Ahsgdfloqifg     (04 Nov 2016, 04:58)
Pgksrjgiohi hw hweokfjeq ojfe jfweiogwo gwoj wijf gdhgtrj575 y6u75tyhgf 5yu5regr
Shalini     (05 Jul 2013, 09:41)
Is there a way to get the last field using cut command? The number of fields could be arbitrary. How can I get the last field in each line?
Craig     (14 Jan 2013, 20:05)
Arnaudlinux, try the following:
cat file | awk 'BEGIN{FS=";"} {for(i=1;i<NF;i++) print $i}' | sort
Mark     (19 Nov 2012, 20:12)
Trying to graph lots of data in MATLAB. Want to pull out sections of 1.5GB file from the middle, not just from head/tail because amount is so large. Possible?
haneen     (07 Oct 2012, 00:30)
how to do this task on fedora :Assume a directory contains many .mp3 files taken from albumes. If you are asked to sort them out into separate folders by artist. How would you do that?
Arnaudlinux     (05 Sep 2012, 21:42)
Hi budies!
Really need some help (and sorry for my English, i'm come frome France),
I'm on ubuntu, bash.
I've got a file with a lot of names delimited with a ";" written like this:

Francois de france; Julien d'amerique; Emilie de Suede; gustave de Picardie;

It's very difficult to read it, cause i have hundreds of them.
I'm trying to sort them like this:

Francois de France;
Julien d'Amerique;
(...)
Gustave de Picardie;

So i tried to use the ";" as a delimiter and reading the arguments:

cut -d ";" -f 1-100 file.txt

It only give me my first style list in the terminal....I'm completly blocked...
Is there here a Linux Champion that can help me..?

Thanks all !!!
Micho     (09 Aug 2012, 17:39)
Hi,

Is there any difference between using -f flag and -c flag for a file given below for instance? Could you also provide an example where it matters which of those 2 flags you use?

AAAA BBBB CCCC
AAAA BBBB CCCC
Radha     (24 Apr 2012, 06:02)
shreeram gundu:
try this..

sed -n -e '1'p -e '$'p shree.txt
amritpal     (01 Apr 2012, 22:22)
i have a question?

Display all of the lines in the file where the last field is 4 digits long.

Search for "," before the field to make sure your regular expression matches
the whole field, use character classes to match four digits, and anchor it
to the end of the line to make sure it's the last field.

ambuja     (14 Mar 2012, 07:28)
was searching for command to get only file names of out put ls -lrt.
got that command..its very usefull
Irfan     (29 Feb 2012, 05:12)
@jeenkar: try this one
cut -d' ' -f3 <file1 >file2
Franco     (22 Feb 2012, 03:48)
You really good
Florin     (13 Feb 2012, 19:04)
Thanks, this is greet!
shreeram gundu     (13 Feb 2012, 01:24)
Hi All,

I have a file like shree.txt. how to fetch the first record and last record by using unix command?

Thanks, Advanced
Bob Rankin     (03 Feb 2012, 13:29)
@Sini - no way directly with the cut command. But you can use the "rev" command to reverse the lines, then cut the FIRST field, then rev again:

rev infile | cut -d'/' -f1 | rev

You can also do it with awk:

awk -F'/' '{print $NF}'
Sini     (03 Feb 2012, 02:06)
How to cut the last field in cut command, i dont know the last field number. For eg: the file contains the below data

/oss/orbit/server/abc.txt
/oss/orbit/server/scripts/abc1.txt

The output should be

abc.txt
abc1.txt
vikas     (27 Jan 2012, 08:17)
How to retrieve information that is under a column example:

s.no name date
1 a 21
2 b 22
3 c 23

i want to retrieve and display all values under name column or sometime name and date columns both.

name
a
b
c

Jimmy     (17 Jan 2012, 01:36)
hi
Please tell me the command to display following name in short manner
John Milan Robort
J M R (short form)
newperlguy     (11 Dec 2011, 20:42)
I need some help. i've got a txt file that is full of numbers separated by spaces. there are groups of numbers in this fashion representing measurements taken from the human body. the page of text has several groups of numbers for each person. I want to take one number group from each section and average them. here is an example of the txt file...

244 1961287 559 302 2091182 273 224 172 353 254 440 68 224 299 325 309 284 283 265 129 185 958 221 769 557 465 350 2751361 629
245 2551501 616 346 2091395 299 231 202 393 299 463 70 256 326 375 322 304 307 295 132 1911009 245 956 656 545 370 3561585 700

there are 5 spaces at the beginning of each group of numbers. how do I average the third number in each group?
jagan     (18 Nov 2011, 06:31)
Is any command to know how many fileds in a line by using cut command or awk?
abnush     (16 Nov 2011, 18:35)
how can i execute this command in cygwin
"ls, ps" together..mean i want to run this commands at the sametime bieng seprated by a commaa?
Prashant     (15 Nov 2011, 07:15)
@Chem for your query(03 Oct 2011, 11:13)
echo "9999 1.7260E+01 3.5783E-03" | awk '{FS = " "} {print $3}'
Kinita     (05 Nov 2011, 17:34)
Can someone tell me what:
ls -l | sed '/^d/d'
this command does?
I ran it in Secure Shell, it looks like it eliminates all the Directories and subdirectories. Am I correct?
Abhay     (27 Oct 2011, 09:05)
there are two files having data in row - column format. now i want to print the data of second column of first file & third column of second file & format will be like this that 1 st output should come in first column & second output come in second column..

Please suggest.
Emilio     (20 Oct 2011, 19:55)
Wow, that was awesome! It saved me from doing it manually on a 2000+ line file. Thank you so much...

And @Chem,, I guess you could do something like
cut -d' ' -f3
since the numbers seem to be separated by spaces (assuming that you treat "9999", "1.7260E+01" and "3.5783E-03" as different numbers.
Chem     (03 Oct 2011, 11:13)
Hi Guys,

Can I cut the third number? If the text is like this:
...
9999 1.7260E+01 3.5783E-03 ...
...
Assuming I don't know how many columns are there for each number.

Thanks for your time!

Chem
Stu     (29 Aug 2011, 16:04)
Joseph,
Don't apologize for your English. Most Amreicans barely speak correct English, let alone a foreign language.
ankit     (12 Aug 2011, 01:17)
sir im using sed command sed -e"${n}d " file..............
n is the variable which contain more than 2 line numbers which i want to delete ....but its not working for more than 2 line numbers...........please give me solution
jeenkar     (29 Jul 2011, 10:46)
hi all

i want to elect only 3rd column and save in different file how can i do..
ATOM 2 CA ASN A 1 0.395 3.556 1.478
ATOM 2 CA ASN A 1 0.395 3.556 1.478
ATOM 2 CA ASN A 1 0.395 3.556 1.478

Bob Rankin     (04 May 2011, 07:17)
@Joseph - The problem is the accented characters that count as two bytes.
Joseph     (27 Apr 2011, 04:07)
Hello!
I'm Joseph. I speak englis a little (I'm Hungarian man). Sorry.
Please help.
The operation system of my desktop machine is Ubuntu linux 10.04, the language is Hu, encoding is utf8 (default).

My example data file is "cities.data, this file have got 1 line, 1 world:

$ cat cities.data
Székesfehérvár

This is OK, it is 14 characters.

The cut program:

$ cut -c 1-14 cities.data
Székesfehérv

It is 12 chars!

$ cut -c 1-2 cities.data
Sz

OK.

$ cut -c 1-3 cities.data
Sz

This is bad.

$ cut -c 3 cities.data

(empty)

$ cut -c 4 cities.data


What is the problem?

Please Help!

Joseph
Ryan     (21 Apr 2011, 09:08)
@Jack really?? Dude, if you're any kind of a sysadmin you'd know that rarely on any given system do you have something like ultraedit installed. STFU and GBTW.
Jack     (05 Apr 2011, 07:47)
Use Ultraedit for pete's sake
Tippu M     (01 Apr 2011, 07:55)
I love this site thanks a lot
rashmi     (27 Mar 2011, 13:17)
the output of a command is
controller: lun-pathname device filename adapter protocol lun size lun state
fas3170-201-77: /vol/sanboot_192_rhel6/sanboot_192 /dev/sdd host6 iSCSI 40.0g (42953867264) GOOD
fas3170-201-77: /vol/sanboot_192_rhel6/sanboot_192 /dev/sdc host5 iSCSI 40.0g (42953867264) GOOD

I want to extract only the words which are /dev/sda or /dev/sdc like that. how can i do this. Please help.
thank you
TY 2020     (11 Mar 2011, 11:49)
I was wondering how you can reorder the data in a file using the cut and cat commands?


sort a comma-separated list
by domain, GET request, username, and/or response code using shell scripting

www.google.com,GET /blah,jeromy,404
www.yahoo.com,GET /index.html,nate,200
www.bing.com,GET /lame_search_engine,lameuser,200
www.aj.com,GET /?,nate,404

get the list sorted by user name with all the fields still in tact.

so have the output look something like:

jeromy www.google.com GET /blah
nate www.yahoo.com GET /index.html
lameuser www.bing.com GET/lame_search_engine
nate www.aj.com GET /?




ken     (07 Mar 2011, 14:42)
how to retrieve the first ROW of each file in a directory
Neede     (05 Mar 2011, 23:38)
How can I have a comma a space after each word and a dot at the end using something like the fallowing command.
echo `grep userone /etc/group | cut -d\: -f 1`

Assume that the user is in several groups and all we want is the output to be something like

nt, it, hr, cos.


I tried piping it through tr but all I got was a comma and no space after each word.
echo`grep userone /etc/group | cut -d\: -f 1 | tr [:sapce:] [,]`
harinik     (10 Feb 2011, 05:45)
i have a text file
i want to print column 22 23 and 52 53 only if column 23 is A and 53 is B
this is from text file how do i do so
Gatha     (02 Feb 2011, 08:02)
Hi,
need a clarfication on the above post.

How to use a Hex code delimiter.

My hex code delimiter is '001' (Ctrl A)

Please help me with the code.

Many Thanks
Regards
Gatha
yogendra Barode     (28 Jan 2011, 12:05)
Thank you help alot
Siv     (29 Dec 2010, 01:04)
i have two coloumns with 3 rows
1 a
2 b
c d
and i want it to be displayed like
1 a
b
c
using sed command.
thnx.

Kenny     (15 Dec 2010, 04:18)
This is a great resource. I repeat a previous comment in that the book takes 3 times as long to explain in a vague way. This is not only useful but something more easily retained for future problems without relying on you.
srinivas     (04 Dec 2010, 10:16)
its good. i got enough information abt linux commands from this

Thanks a lot
ram     (03 Dec 2010, 03:47)
i have text like this
doc1.doc

i want to change it to DOC1.doc (change only the name to uppercase)
plz help
Bob Rankin     (24 Nov 2010, 08:03)
@vamsi - To take only the second line, do this:

head -2 | tail -1
vamsi     (21 Nov 2010, 00:23)
hi frd
can i able to cut a single row from an n rows output
for example
#cat /proc/cpuinfo | grep core
gives the output
ore id : 0
cpu cores : 2
core id : 2
cpu cores : 2
core id : 0
cpu cores : 2
core id : 2
cpu cores : 2


i need to cut the 2nd row only...what command shd i use

CJ     (19 Nov 2010, 11:29)
I want to assemble a file name using the date at the start and some static text on the right so I can use it in a SED command that will concatenate two files together, but I only want the left 9 characters of the timestamp() (year and month). So far I have:

sed '$r Report.htm' <Header.htm> s_timestamp() + Monthly_Report.htm

Which I know will not work right. What command do I use to get the left 9 bytes from the timestamp? how do I append it to the name of the concatenated report?

Anyhelp would be great, thanks!
gaiyathri     (27 Oct 2010, 07:27)
e|
|b|c
wen two pipeline is encountered it should eliminate & return the result as
e|b|c
moemedi     (21 Oct 2010, 08:42)
i want to calculate and display any keyboard entered interger into an output of following example in c
example
7322 seconds is equivalent to 2 hours 2 minutes 2 seconds.

Bob Rankin     (18 Oct 2010, 11:03)
@Poly - Use the cut command to pull out just the column with the usernames, then append the extra text with awk. Something like this:

cut -d':' -f1 /etc/passwd | awk '{print $1"@example.com"}'

@Poly     (18 Oct 2010, 08:07)
How do I extract logon names from the /etc/passwd file and then add the domain @example.com
like adm@example.com?
Mr.hung     (09 Oct 2010, 09:50)
i have a text
12131313 ??? asdadsa cmd
132213 ?? fafa ls
i want show result:
12131313 cmd
132213 ls
Bob Rankin     (07 Oct 2010, 10:52)
@moemedi - That sounds suspiciously like a homework assignment. You're on your own from here on! :-)
moemedi     (07 Oct 2010, 10:38)
the following is bill information
the 1st column is account and the 2 last columns need to be displayed but only for the first account
10000000|Jan|02-Feb-2006|5753|5855
10000001|Jan|03-Feb-2006|6397|6567
10000000|Feb|04-Mar-2006|5855|6060
10000001|Feb|27-Feb-2006|6567|6597
10000000|Mar|02-Apr-2006|6060|6320
10000001|Mar|01-Apr-2006|6597|6639
10000000|Apr|01-May-2006|6320|6384
10000001|Apr|02-May-2006|6639|6646

output
Month Consumption
----- -----------
Jan xxxxxx
Feb xxxxxx
Mar xxxxxx
Apr xxxxxx
May xxxxxx
Jun xxxxxx
--------------------------------
Total xxxxxx
the "XXX" has 2 be automatically filled by the code
Bob Rankin     (07 Oct 2010, 09:57)
@AC - To extract the first letter from each line, use cut with the -b option: cut -b1
Bob Rankin     (07 Oct 2010, 09:54)
@moemedi - Just use sed to change the delimiter to blank. sed 's/|/ /g'
moemedi     (07 Oct 2010, 09:43)
how do print columns without printing the delimter?
example
23|bak|234|54
34|col|453|65
i want the result to be without the delimiter in between
ie:
234 54
453 65

AC     (07 Oct 2010, 08:27)
I want to extract 1st letter from ABDSFGSDFS (any word). Can any one please help me ?
satyanarayana     (27 Sep 2010, 02:37)
your explanation was superb
Younis     (06 Sep 2010, 21:29)
I love this site, your the best Doctor Bob
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.

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 
 


Ask Bob Rankin - Free Tech Support


Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.