Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Linux & Mac & Bash Command Dictionary

  1. Home
  2. Linux & Mac & Bash Command Dictionary
  3. cut / paste / join

cut / paste / join

Since: All Linux
macOS(2001 Cheetah)
Bash 1.0(1989)

cut extracts specific columns or character ranges from each line of a file, paste merges lines from multiple files side by side, and join combines two files based on a common field as the key.

Syntax

cut [options] [file...]
paste [options] [file...]
join [options] file1 file2

Options

Command / OptionDescription
cut -d delimiter -f fieldSplits fields by the delimiter and extracts the specified field number.
cut -f 1,3Extracts the 1st and 3rd fields.
cut -f 1-3Extracts fields 1 through 3.
cut -c 1-5Extracts characters 1 through 5 from each line.
paste file1 file2Merges corresponding lines from two files, separated by a tab.
paste -d ',' file1 file2Merges lines using the specified delimiter.
paste -s fileJoins all lines of a file into a single tab-separated line.
join file1 file2Performs an inner join using the first column of each file as the key (files must be pre-sorted).
join -1 N -2 M file1 file2Joins using column N of file1 and column M of file2 as the key.
join -a 1 file1 file2Also outputs unmatched lines from file1 (left outer join).

Sample Code

The following files are used in the examples below.

data.csv
user_a,28,Tokyo,engineer
user_b,15,Osaka,designer
user_c,15,Nagoya,manager
names.txt
user_a
user_b
user_c
scores.txt
85
92
78
users.txt (sorted by ID)
001 user_a
002 user_b
003 user_c
roles.txt (sorted by ID)
001 engineer
002 designer
003 manager

Use cut -d -f to specify a delimiter and field number to extract a column.

cut -d',' -f1,3 data.csv
user_a,Tokyo
user_b,Osaka
user_c,Nagoya

Use cut -f 1-2 to extract a range of fields.

cut -d',' -f1-2 data.csv
user_a,28
user_b,15
user_c,15

Use cut -c to extract characters by position.

cut -c1-5 data.csv
user_
user_
user_

Use paste to merge two files side by side (tab-separated).

paste names.txt scores.txt
user_a	85
user_b	92
user_c	78

Use paste -d to specify a custom delimiter.

paste -d',' names.txt scores.txt
user_a,85
user_b,92
user_c,78

Use paste -s to join all lines of a file into a single line.

paste -s names.txt
user_a	user_b	user_c

Use join to combine two files based on a shared field (files must be sorted in advance).

join users.txt roles.txt
001 user_a engineer
002 user_b designer
003 user_c manager

Common Mistakes

Common Mistake 1: Confusing -f (field) and -c (character)

-f selects by field (delimiter-separated) and -c selects by character position. Using the wrong option produces unexpected output.

echo "id:name:age" | cut -c2
d
(retrieves the 2nd character, not the 2nd field)

Use -f with -d to extract by field.

echo "id:name:age" | cut -d: -f2
name

Common Mistake 2: cut cannot handle CSV fields that contain the delimiter

If a field is quoted and contains the delimiter, cut splits at every occurrence regardless of quoting.

echo '"data_a, location",28' | cut -d, -f1
"data_a
(split inside the quoted field)

Use awk or a dedicated CSV tool for fields that may contain the delimiter.

Notes

'$ cut' is convenient for simple field extraction, but it cannot handle cases where a field contains the delimiter character (such as quoted CSV). For more complex cases like these, use awk.

For counting or sorting fields, see also sort.

If you find any errors or copyright issues, please .