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.

  1. Home
  2. Bash Dictionary
  3. cut / paste / join

cut / paste / join

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
alice,30,Tokyo,engineer
bob,25,Osaka,designer
charlie,35,Nagoya,manager
names.txt
alice
bob
charlie
scores.txt
85
92
78
users.txt (sorted by ID)
001 alice
002 bob
003 charlie
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
alice,Tokyo
bob,Osaka
charlie,Nagoya

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

cut -d',' -f1-2 data.csv
alice,30
bob,25
charlie,35

Use cut -c to extract characters by position.

cut -c1-5 data.csv
alice
bob,2
charl

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

paste names.txt scores.txt
alice	85
bob	92
charlie	78

Use paste -d to specify a custom delimiter.

paste -d',' names.txt scores.txt
alice,85
bob,92
charlie,78

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

paste -s names.txt
alice	bob	charlie

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

join users.txt roles.txt
001 alice engineer
002 bob designer
003 charlie manager

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 .