Transpose File
Problem
Given a text file file.txt containing whitespace-separated fields, print its transposed contents — column j of every row j of the input becomes row j of the output. All rows have the same number of columns. Canonical awk: awk '{for(i=1;i<=NF;i++)a[NR,i]=$i} END{for(i=1;i<=NF;i++){for(j=1;j<=NR;j++)printf "%s%s",a[j,i],j==NR?"\n":" "}}'.
"name age\nalice 21\nryan 30""name alice ryan\nage 21 30"# Bash one-liner:
# awk '{for(i=1;i<=NF;i++)a[NR,i]=$i}
# END{for(i=1;i<=NF;i++){for(j=1;j<=NR;j++)
# printf "%s%s",a[j,i],j==NR?"\n":" "}}' file.txt
def transpose(lines):
grid = [line.split() for line in lines]
cols = len(grid[0])
return [' '.join(grid[r][c] for r in range(len(grid))) for c in range(cols)]
// Bash one-liner:
// awk '{for(i=1;i<=NF;i++)a[NR,i]=$i}
// END{for(i=1;i<=NF;i++){for(j=1;j<=NR;j++)
// printf "%s%s",a[j,i],j==NR?"\n":" "}}' file.txt
function transpose(lines) {
const grid = lines.map(l => l.split(/\s+/).filter(Boolean));
const cols = grid[0].length;
const out = [];
for (let c = 0; c < cols; c++) {
const row = [];
for (let r = 0; r < grid.length; r++) row.push(grid[r][c]);
out.push(row.join(' '));
}
return out;
}
// Bash one-liner:
// awk '{for(i=1;i<=NF;i++)a[NR,i]=$i} END{...}' file.txt
class Solution {
public List<String> transpose(List<String> lines) {
String[][] grid = new String[lines.size()][];
for (int i = 0; i < lines.size(); i++) grid[i] = lines.get(i).trim().split("\\s+");
int cols = grid[0].length;
List<String> out = new ArrayList<>();
for (int c = 0; c < cols; c++) {
StringBuilder sb = new StringBuilder();
for (int r = 0; r < grid.length; r++) {
if (r > 0) sb.append(' ');
sb.append(grid[r][c]);
}
out.add(sb.toString());
}
return out;
}
}
// Bash one-liner:
// awk '{for(i=1;i<=NF;i++)a[NR,i]=$i} END{...}' file.txt
vector<string> transpose(vector<string>& lines) {
vector<vector<string>> grid(lines.size());
for (int i = 0; i < (int)lines.size(); i++) {
stringstream ss(lines[i]);
string tok;
while (ss >> tok) grid[i].push_back(tok);
}
int cols = grid[0].size();
vector<string> out;
for (int c = 0; c < cols; c++) {
string row;
for (int r = 0; r < (int)grid.size(); r++) {
if (r) row += ' ';
row += grid[r][c];
}
out.push_back(row);
}
return out;
}
Explanation
Transposing a file means turning its columns into rows: whatever was the first column of every line becomes the first output line. The plan is to read everything into a 2D grid, then print it column by column.
First we split each input line on whitespace, giving grid, a list of rows where grid[r][c] is the field at row r, column c. Since every row has the same number of columns, we grab that count as cols.
Then for each column index c we walk down all rows r and collect grid[r][c], joining those with spaces into one output line. This is exactly the transpose, because the new row c is built from old column c.
The awk one-liner does the same thing: it stores every field in a 2D array a[NR,i], then in its END block prints the array with the row and column loops swapped.
Example: input rows name age, alice 21, ryan 30. Column 0 is name, alice, ryan and column 1 is age, 21, 30, so the output is "name alice ryan" and "age 21 30".