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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code