100 Useful Command-Line Utilities

by Oliver; 2014

39. cut

cut cuts one or more columns from a file, and delimits on tab by default. Suppose a file, sample.blast.txt, is:
TCONS_00007936|m.162    gi|27151736|ref|NP_006727.2|    100.00  324
TCONS_00007944|m.1236   gi|55749932|ref|NP_001918.3|    99.36   470
TCONS_00007947|m.1326   gi|157785645|ref|NP_005867.3|   91.12   833
TCONS_00007948|m.1358   gi|157785645|ref|NP_005867.3|   91.12   833 
Then:
$ cat sample.blast.txt | cut -f2
gi|27151736|ref|NP_006727.2|
gi|55749932|ref|NP_001918.3|
gi|157785645|ref|NP_005867.3|
gi|157785645|ref|NP_005867.3|
You can specify the delimiter with the -d flag, so:
$ cat sample.blast.txt | cut -f2 | cut -f4 -d"|" 
NP_006727.2
NP_001918.3
NP_005867.3
NP_005867.3
although this is long-winded and in this case we can achieve the same result simply with:
$ cat sample.blast.txt | cut -f5 -d"|" 
NP_006727.2
NP_001918.3
NP_005867.3
NP_005867.3
Don't confuse cut with its non-unix namesake on Macintosh, which deletes text while copying it to the clipboard.

Tip: If you're a Vim user, running cut as a system command within Vim is a neat way to filter text. Read more: Wiki Vim - System Commands in Vim.

<PREV   NEXT>