Transpose File - Problem

You're tasked with transposing a text file - essentially rotating it 90 degrees so that rows become columns and columns become rows. This is a classic shell scripting challenge that tests your ability to manipulate structured text data.

The Challenge: Given a text file file.txt where each row contains the same number of space-separated fields, transform it so that:

  • The 1st column of all rows becomes the 1st row
  • The 2nd column of all rows becomes the 2nd row
  • And so on...

Example: If your file contains:
name age city
john 25 paris
jane 30 london

The transposed output should be:
name john jane
age 25 30
city paris london

This operation is fundamental in data processing, similar to transposing a matrix in mathematics or pivoting data in spreadsheets.

Input & Output

Basic Employee Data
$ Input: name age city john 25 paris jane 30 london
โ€บ Output: name john jane age 25 30 city paris london
๐Ÿ’ก Note: The first column (name, john, jane) becomes the first row, the second column (age, 25, 30) becomes the second row, and so on.
Numeric Matrix
$ Input: 1 2 3 4 5 6 7 8 9
โ€บ Output: 1 4 7 2 5 8 3 6 9
๐Ÿ’ก Note: A 3x3 matrix transposition where element at position (i,j) moves to position (j,i).
Single Row
$ Input: a b c d e
โ€บ Output: a b c d e
๐Ÿ’ก Note: A single row with 5 elements becomes 5 rows with 1 element each - converting horizontal data to vertical.

Visualization

Tap to expand
File Transposition ProcessOriginal File (Rows โ†’ Columns)name age cityjohn 25 parisjane 30 londonMatrix Storagea[row,col]Transpose LogicTransposed Output (Columns โ†’ Rows)name john janeage 25 30city paris london90ยฐ Rotation TransformKey Insight: Read row-wise, output column-wiseAWK Solution: for(col=1; col<=max_cols; col++) { for(row=1; row<=max_rows; row++) printf a[row,col] }๐ŸŽฏ Perfect for: Data analysis, CSV processing, report formatting๐Ÿ’ก Shell tools like AWK excel at text transformation tasks like this
Understanding the Visualization
1
Read Original
Input file with rows and columns of data
2
Store in Matrix
Each field stored at position [row,col] in memory
3
Output by Columns
Read stored data column-wise to create transposed rows
Key Takeaway
๐ŸŽฏ Key Insight: File transposition is fundamentally about changing your iteration order - instead of processing row-by-row, you collect and output column-by-column. AWK's associative arrays make this transformation elegant and efficient.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nร—m)

Where n is number of rows and m is number of columns - we read each cell twice

n
2n
โœ“ Linear Growth
Space Complexity
O(nร—m)

Need to store the entire file content in memory as arrays

n
2n
โšก Linearithmic Space

Constraints

  • Each row has the same number of columns
  • Fields are separated by single space character ' '
  • File is not empty and contains at least one row
  • Maximum file size should be reasonable for shell processing
  • Shell environment: bash, awk, or similar Unix tools available
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 10
28.5K Views
Medium Frequency
~15 min Avg. Time
890 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