Paste (Unix): Difference between revisions

Content deleted Content added
→‎History: Original version author
 
(39 intermediate revisions by 27 users not shown)
Line 1:
{{Unreferenced|date=December 2009}}
{{Lowercase|title=paste (Unix)}}
{{Infobox software
'''paste''' is a [[Unix]] [[command line]] utility which is used to join files horizontally (parallel merging) by outputting lines consisting of the sequentially corresponding lines of each file specified, separated by tabs, to the [[standard output]]. It is effectively the horizontal equivalent to the utility [[cat (Unix)|cat]] command which operates on the vertical plane of two or more files.
| name = paste
| logo =
| screenshot =
| screenshot size =
| caption =
| author =
| developer =
| released =
| latest release version =
| latest release date =
| operating system = [[Unix]] and [[Unix-like]]
| platform = [[Cross-platform]]
| genre = [[Command (computing)|Command]]
| license = [[coreutils]]: [[GPLv3+]]
| website =
}}
'''paste''' is a [[Unix]] [[Command-line interface|command line]] utility which is used to join files horizontally (parallel merging) by outputting lines consisting of the sequentially corresponding lines of each file specified, separated by tabs, to the [[standard output]].
 
==ExampleHistory==
The original [[Bell Labs]] version was written by [[Gottfried W. R. Luderer]].<ref>{{Cite web|url=https://man.openbsd.org/paste.1|title=paste(1) - OpenBSD manual pages}}</ref><ref>{{Cite web|url=https://www.tuhs.org/pipermail/tuhs/2020-January/019955.html|title=[TUHS] A portrait of cut(1)}}</ref> The version of <code>paste</code> bundled in [[GNU]] [[coreutils]] was written by David M. Ihnat and David MacKenzie.<ref>{{Cite web|url=https://linux.die.net/man/1/paste|title=Paste(1): Merge lines of files - Linux man page}}</ref> The command is available as a separate package for [[Microsoft Windows]] as part of the [[UnxUtils]] collection of [[Native (computing)|native]] [[Windows API|Win32]] [[porting|ports]] of common GNU Unix-like utilities.<ref>{{Cite web|url=http://unxutils.sourceforge.net/|title=Native Win32 ports of some GNU utilities|website=unxutils.sourceforge.net}}</ref>
To paste several columns of data together into the file '''www''' from files '''who''', '''where''', and '''when''':
 
==Usage==
paste who where when > www
The {{Mono|paste}} utility is invoked with the following syntax:
 
paste [''options''] [''file1'' ..]
If the files contain:
 
==Description==
{| class="wikitable"
Once invoked, {{Mono|paste}} will read all its ''{{Mono|file}}'' arguments. For each corresponding line, {{Mono|paste}} will append the contents of each file at that line to its output along with a tab. When it has completed its operation for the last file, {{Mono|paste}} will output a newline character and move on to the next line.
|-
! who
! where
! when
|-
|
Sam <br />
Dave <br />
Sue
|
Detroit <br />
Edgewood <br />
Tampa
|
January 3 <br />
February 4 <br />
March 19
|}
 
{{Mono|paste}} exits after all streams return end of file. The number of lines in the output stream will equal the number of lines in the input file with the largest number of lines. Missing values are represented by empty strings.
This creates the file named '''www''' containing:
 
Sam Detroit January 3
Though potentially useful, an option to have paste emit an alternate string for a missing field (such as "NA") is not standard.
Dave Edgewood February 4
 
Sue Tampa March 19
A sequence of empty records at the bottom of a column of the output stream may or may not have been present in the input file corresponding to that column as explicit empty records, unless you know the input file supplied all rows explicitly (e.g. in the canonical case where all input files all do indeed have the same number of lines).
 
==Options==
The {{Mono|paste}} utility accepts the following options:
 
<code>-d|--delimiters ''delimiters''</code>, which specifies a list of [[delimiter]]s to be used instead of tabs for separating consecutive values on a single line. Each delimiter is used in turn; when the list has been exhausted, {{Mono|paste}} begins again at the first delimiter.
 
<code>-s|--serial</code>, which causes {{Mono|paste}} to append the data in serial rather than in parallel; that is, in a horizontal rather than vertical fashion.
 
==Examples==
For the following examples, assume that {{Mono|names.txt}} is a plain-text file that contains the following information:
 
<pre>Mark Smith
Bobby Brown
Sue Miller
Jenny Igotit</pre>
 
and that {{Mono|numbers.txt}} is another plain-text file that contains the following information:
 
<pre>555-1234
555-9876
555-6743
867-5309</pre>
 
The following example shows the invocation of {{Mono|paste}} with {{Mono|names.txt}} and {{Mono|numbers.txt}} as well as the resulting output:
 
<syntaxhighlight lang="console">$ paste names.txt numbers.txt
Mark Smith 555-1234
Bobby Brown 555-9876
Sue Miller 555-6743
Jenny Igotit 867-5309</syntaxhighlight>
 
When invoked with the {{code|--serialize}} option ({{code|-s}} on BSD or older systems), the output of {{Mono|paste}} is adjusted such that the information is presented in a horizontal fashion:
 
<syntaxhighlight lang="console">$ paste --serialize names.txt numbers.txt
Mark Smith Bobby Brown Sue Miller Jenny Igotit
555-1234 555-9876 555-6734 867-5309</syntaxhighlight>
 
Finally, the use of the {{code|--delimiters}} option ({{code|-d}} on BSD or older systems) is illustrated in the following example:
 
<syntaxhighlight lang="console">$ paste --delimiters . names.txt numbers.txt
Mark Smith.555-1234
Bobby Brown.555-9876
Sue Miller.555-6743
Jenny Igotit.867-5309</syntaxhighlight>
 
As an example usage of both, the {{Mono|paste}} command can be used to concatenate multiple consecutive lines into a single row:
 
<syntaxhighlight lang="console">$ paste --serialize --delimiters '\t\n' names.txt
Mark Smith Bobby Brown
Sue Miller Jenny Igotit
</syntaxhighlight>
 
==See also==
*[[join (Unix)|join]]
*[[cut (Unix)|cut]]
*[[List of Unix programscommands]]
*lam(1), formatted paste with width and justification; not core Unix
 
==References==
{{Reflist}}
 
==External links==
{{Wikibooks|Guide to Unix|Commands}}
* {{cite web|url=http://www.freebsd.org/cgi/man.cgi?query=paste&section=1&format=html
|access-date=2010-08-12
|title=PASTE(1) - FreeBSD General Commands Manual
}}
 
{{Unix commands}}
{{Core Utilities commands}}
 
[[Category:Unix text processing utilities]]
[[Category:Unix SUS2008 utilities]]
 
[[it:Paste]]
[[hu:Paste (Unix)]]
[[pl:Paste]]
[[pt:Paste (Unix)]]
[[ru:Paste]]
[[uk:Paste]]