Text Transforms

Reshape data between columns and rows — paste -sd, for collapsing, tr for expanding, awk for complex transforms.

paste — Column to Row

The primary use: collapse a column of values into a single comma-separated row.

Collapse VLAN list from a file
paste -sd, /tmp/vlans.txt
# → 144,146,621,721,1551,1651,1921

-s = serial (concatenate all lines into one). -d, = delimiter is comma.

Without -s, paste merges columns from multiple files
paste file1.txt file2.txt    # Side-by-side columns (tab-delimited)
paste -d, file1.txt file2.txt  # Comma-delimited columns

The -s flag changes the axis: without it, paste works horizontally across files. With it, paste works vertically within a single file.

Network Engineering Pipeline

The real-world pattern: extract VLANs from switch output → collapse into trunk allowed-list format.

Extract VLAN IDs from show vlan brief and collapse
# From a file
awk 'NR>2 && /^[0-9]+/{print $1}' show-vlan-brief.txt | paste -sd,
# → 144,146,621,721,1551,1651,1921

# From an SSH session (no temp file)
ssh admin@switch "show vlan brief" \
  | awk 'NR>2 && /^[0-9]+/{print $1}' \
  | paste -sd,
Use the result directly in a switch command
VLANS=$(ssh admin@switch "show vlan brief" | awk 'NR>2 && /^[0-9]+/{print $1}' | paste -sd,)
echo "switchport trunk allowed vlan ${VLANS}"

Alternatives Compared

Tool Command Trailing Delimiter?

paste -sd,

paste -sd, file.txt

No ✅

tr

tr '\n' ',' < file.txt

Yes ❌ (trailing comma)

awk (ORS)

awk '{printf "%s%s", sep, $0; sep=","} END{print ""}' file.txt

No ✅ (verbose)

awk (NR)

awk 'NR>1{printf ","} {printf "%s", $0} END{print ""}' file.txt

No ✅ (verbose)

paste -sd, wins: no trailing delimiter, no printf gymnastics, one command.

Row to Column (Reverse)

Expand comma-separated row back to column
echo "144,146,621,721,1551,1651,1921" | tr ',' '\n'
Or with awk (handles any delimiter)
echo "144,146,621,721,1551,1651,1921" | awk -F, '{for(i=1;i<=NF;i++) print $i}'

Other paste Patterns

Number lines by pasting line numbers alongside content
paste <(seq $(wc -l < file.txt)) file.txt
Interleave two files (alternating lines)
paste -d'\n' questions.txt answers.txt
Create key=value pairs from two columns
paste -d= keys.txt values.txt
Group every N lines into one row (2 lines per row, tab-delimited)
paste - - < file.txt
# Three per row:
paste - - - < file.txt

Delimiter Variations

Semicolon-separated
paste -sd';' file.txt
Space-separated
paste -sd' ' file.txt
Newline-to-pipe (for regex alternation)
paste -sd'|' patterns.txt
# → pattern1|pattern2|pattern3  (ready for grep -E)