cut in Linux (nice but don’t cut your finger)

cut command- Divide a file into several parts

Syntax: cut [OPTION]… [FILE]…

Description: Prints selected parts of lines (of a text file), or, in other words, removes certain sections of a line. You may wish to remove things according to tabs or commas, or anything else you can think of…

cut is nice linux commend, it can extract a certain part from each line of a file.

Consider a slight variation on the foo.txt file we’ve been playing with in this section:

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:

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 foo.txt
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 foo.txt
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 foo.txt
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 get columns from 2 to end  and put in to file out.txt  cut -d’ ‘ -f2- test.txt > out.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.

 

 

 

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.

About these ads

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