CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Text Processing / paste

paste

Merge lines of files side by side, or join lines into one

Beginnercolumnspipelinesjoin

paste glues files together column by column: line 1 of each file becomes line 1 of the output, separated by tabs. With -s it does the opposite job and joins all lines of one file into a single line, which is how you build comma lists, regex alternations or a sum expression.

Syntaxpaste [-d DELIMS] [-s] [FILE]... (- means stdin)

Watch it run

You have a host list and a matching list of zones and IPs from different exports, and want one table.

devops@devops-lab: ~/data — zsh — 96×28
➜ ~/data
Step 1 of 3
paste hosts.txt zones.txt

Line N of hosts.txt is joined to line N of zones.txt with a TAB. paste trusts the line order completely: if one file is sorted differently, rows get mismatched silently.

Anatomy of a command

$
commandcut -d' ' -f10 access.log

Produce one number per line: the response size in bytes from each nginx log line.

How it works

paste opens every file you list (a - means stdin, and can appear more than once) and then reads them in lockstep: one line from the first file, one from the second, and so on, writing them out joined by the delimiter and ending with a newline. When a file runs out early it contributes an empty field, so the delimiters remain and the columns stay aligned. The -d list is used cyclically: -d',;' alternates between comma and semicolon. With -s, paste processes each file on its own instead, replacing every newline but the last with the delimiter, so a whole file becomes one line. paste never looks at the content of a line, so there's no notion of keys: to merge rows by a matching ID you need join (on sorted input) or awk.

Options you'll actually use

(no option)Merge files side by side, separated by TAB
-d LISTUse these delimiter characters instead of TAB (cycled if several)
-sSerial: join all lines of each file into one line
-Read stdin; paste - - reads two lines at a time into two columns
-zLines are NUL-terminated instead of newline-terminated

Common mistakes

paste ids.txt names.txt when the files are in different orders

paste matches by line number only, not by content. Unsorted or filtered inputs silently pair the wrong rows.

join <(sort ids.txt) <(sort names.txt) # merge on a shared key
paste -d', ' a b expecting ', ' between columns

-d is a list of single delimiters used in turn, so you get a comma between a and b, and a space would be used for the next pair.

paste -d, a b | sed 's/,/, /g'
for h in $(cat hosts); do list="$list$h,"; done

Slow, word-splits on spaces and leaves a trailing comma that breaks many tools.

list=$(paste -sd, hosts)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print all the hosts in ~/data/hosts.txt on one line separated by spaces.

  2. 2

    Show the first field of /etc/passwd and the last field side by side, separated by a TAB, using cut and paste.

  3. 3

    Sum the numbers 1 to 100 using seq and paste.

Related commands

cutCut columns or character ranges out of each lineawkProcess text by columns: filter, sum, count and reporttrTranslate, squeeze or delete charactersxargsBuild and run commands from standard input
PrevioustrNexttee
Linux / Text Processing / paste

paste

Merge lines of files side by side, or join lines into one

Beginnercolumnspipelinesjoin

paste glues files together column by column: line 1 of each file becomes line 1 of the output, separated by tabs. With -s it does the opposite job and joins all lines of one file into a single line, which is how you build comma lists, regex alternations or a sum expression.

Syntaxpaste [-d DELIMS] [-s] [FILE]... (- means stdin)

Watch it run

You have a host list and a matching list of zones and IPs from different exports, and want one table.

devops@devops-lab: ~/data — zsh — 96×28
➜ ~/data
Step 1 of 3
paste hosts.txt zones.txt

Line N of hosts.txt is joined to line N of zones.txt with a TAB. paste trusts the line order completely: if one file is sorted differently, rows get mismatched silently.

Anatomy of a command

$
commandcut -d' ' -f10 access.log

Produce one number per line: the response size in bytes from each nginx log line.

How it works

paste opens every file you list (a - means stdin, and can appear more than once) and then reads them in lockstep: one line from the first file, one from the second, and so on, writing them out joined by the delimiter and ending with a newline. When a file runs out early it contributes an empty field, so the delimiters remain and the columns stay aligned. The -d list is used cyclically: -d',;' alternates between comma and semicolon. With -s, paste processes each file on its own instead, replacing every newline but the last with the delimiter, so a whole file becomes one line. paste never looks at the content of a line, so there's no notion of keys: to merge rows by a matching ID you need join (on sorted input) or awk.

Options you'll actually use

(no option)Merge files side by side, separated by TAB
-d LISTUse these delimiter characters instead of TAB (cycled if several)
-sSerial: join all lines of each file into one line
-Read stdin; paste - - reads two lines at a time into two columns
-zLines are NUL-terminated instead of newline-terminated

Common mistakes

paste ids.txt names.txt when the files are in different orders

paste matches by line number only, not by content. Unsorted or filtered inputs silently pair the wrong rows.

join <(sort ids.txt) <(sort names.txt) # merge on a shared key
paste -d', ' a b expecting ', ' between columns

-d is a list of single delimiters used in turn, so you get a comma between a and b, and a space would be used for the next pair.

paste -d, a b | sed 's/,/, /g'
for h in $(cat hosts); do list="$list$h,"; done

Slow, word-splits on spaces and leaves a trailing comma that breaks many tools.

list=$(paste -sd, hosts)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print all the hosts in ~/data/hosts.txt on one line separated by spaces.

  2. 2

    Show the first field of /etc/passwd and the last field side by side, separated by a TAB, using cut and paste.

  3. 3

    Sum the numbers 1 to 100 using seq and paste.

Related commands

cutCut columns or character ranges out of each lineawkProcess text by columns: filter, sum, count and reporttrTranslate, squeeze or delete charactersxargsBuild and run commands from standard input
PrevioustrNexttee