pasteMerge lines of files side by side, or join lines into one
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.
paste [-d DELIMS] [-s] [FILE]... (- means stdin)You have a host list and a matching list of zones and IPs from different exports, and want one table.
cut -d' ' -f10 access.logProduce one number per line: the response size in bytes from each nginx log line.
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.
(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-terminatedpaste ids.txt names.txt when the files are in different orderspaste 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 keypaste -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,"; doneSlow, word-splits on spaces and leaves a trailing comma that breaks many tools.
list=$(paste -sd, hosts)Try these on a Linux machine of your own. Each task has a hint and a solution.
Print all the hosts in ~/data/hosts.txt on one line separated by spaces.
Show the first field of /etc/passwd and the last field side by side, separated by a TAB, using cut and paste.
Sum the numbers 1 to 100 using seq and paste.
pasteMerge lines of files side by side, or join lines into one
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.
paste [-d DELIMS] [-s] [FILE]... (- means stdin)You have a host list and a matching list of zones and IPs from different exports, and want one table.
cut -d' ' -f10 access.logProduce one number per line: the response size in bytes from each nginx log line.
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.
(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-terminatedpaste ids.txt names.txt when the files are in different orderspaste 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 keypaste -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,"; doneSlow, word-splits on spaces and leaves a trailing comma that breaks many tools.
list=$(paste -sd, hosts)Try these on a Linux machine of your own. Each task has a hint and a solution.
Print all the hosts in ~/data/hosts.txt on one line separated by spaces.
Show the first field of /etc/passwd and the last field side by side, separated by a TAB, using cut and paste.
Sum the numbers 1 to 100 using seq and paste.