Problem Statement
Explain the paste command and how it differs from cat. Provide examples of merging files side-by-side and with custom delimiters.
Explanation
Paste merges lines of files side-by-side separated by tabs, unlike cat which concatenates files vertically (one after another). Paste is useful for combining data from multiple files column-wise, like joining parallel arrays or combining related data from separate sources.
Basic usage: paste file1.txt file2.txt places lines from file1 and file2 side-by-side separated by tab. If files have different lengths, paste continues until longest file ends, leaving empty fields for shorter files. Example: paste names.txt ages.txt creates two-column output combining names with ages.
Custom delimiter with -d: paste -d',' file1.txt file2.txt uses comma instead of tab, creating CSV format. Multiple delimiters: paste -d',;' file1.txt file2.txt file3.txt uses comma between first and second file, semicolon between second and third. Use -d'\n' to merge files line-by-line alternating.
Single file operations: paste -s file.txt merges all lines into one line separated by tabs (serial merge). Paste -s -d',' file.txt creates comma-separated single line from multiple lines. Practical example: paste <(cut -d',' -f1 data.csv) <(cut -d',' -f3 data.csv) extracts and combines columns 1 and 3. Understanding paste enables merging datasets and creating structured output from multiple sources.
