cut

Reads a text file and splits it by a specific string. Extract the values of a specific column from a tabbed file, for example.

NAME
     cut -- cut out selected portions of each line of a file

SYNOPSIS
     cut -b list [-n] [file ...] cut -c list [file ...
     cut -c list [file ...] cut -f list [-d delim] [-s
     cut -f list [-d delim] [-s] [file ...]]

github

  • The file in jupyter notebook format on github is here.

google colaboratory

  • To run it in google colaboratory here cut_nb.ipynb)

environment

The author’s OS is macOS, and the options are different from those of Linux and Unix commands.

If you want to run the command in real life, you need to replace the leading ! and %%bash in the first line.

!sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.6
BuildVersion: 18G95
!bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

Example usage

Typical options

  • d : Specify the delimiter.
  • f : Selects the number of characters to extract from the delimiter.

Create a test file. Creates a test file, tab-delimited.

%%bash
echo -e "a\tb\nc\td" > temp1.txt
cat -t temp1.txt
a^Ib
c^Id

Display the first column.

%%bash
cut -f 1 temp1.txt
a
c

Display the second column.

%%bash
cut -f 2 temp1.txt
b
d

Creates a space-delimited file.

%%bash
echo -e "a b\nc d" > temp2.txt
cat temp2.txt
a b
c d
%%bash
cut -d ' ' -f 1 temp2.txt
a
c
%%bash
cut -d ' ' -f 2 temp2.txt
b
d