Transpose File - Problem

Given a text file file.txt, transpose its content. You may assume that each row has the same number of columns, and each field is separated by the ' ' (space) character.

For example: If file.txt has the content:

name age
john 33
mike 80

Your script should output:

name john mike
age 33 80

Note: This is a shell scripting problem that requires reading and processing file content line by line.

Input & Output

Example 1 — Basic Transpose
$ Input: name age\njohn 33\nmike 80
Output: name john mike\nage 33 80
💡 Note: First row becomes first column: 'name john mike'. Second row becomes second column: 'age 33 80'.
Example 2 — Three Columns
$ Input: a b c\n1 2 3\nx y z
Output: a 1 x\nb 2 y\nc 3 z
💡 Note: Column 1: a,1,x become row 1. Column 2: b,2,y become row 2. Column 3: c,3,z become row 3.
Example 3 — Two Rows Only
$ Input: hello world\nfoo bar
Output: hello foo\nworld bar
💡 Note: Transpose 2x2 matrix: first column 'hello foo', second column 'world bar'.

Constraints

  • Each row has the same number of columns
  • Fields are separated by single space character
  • No empty lines in input
  • Maximum 1000 rows and 100 columns

Visualization

Tap to expand
File Transpose: Rows ↔ ColumnsOriginal File:name agejohn 33mike 803 rows × 2 columnsTRANSPOSERotate 90°Transposed File:name john mikeage 33 802 rows × 3 columnsColumn 1:name, john, mikeColumn 2:age, 33, 80Each original column becomes a new row!
Understanding the Visualization
1
Input
Text file with rows and columns separated by spaces
2
Process
Convert rows to columns and columns to rows
3
Output
Transposed content where original columns become rows
Key Takeaway
🎯 Key Insight: Transpose converts columns to rows by collecting all values from each column position across all rows
Asked in
Google 25 Amazon 18 Microsoft 15
12.5K Views
Medium Frequency
~15 min Avg. Time
350 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen